Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset fatal error in 5.6 but fine in 7

Tags:

php

This snippet of code causes a PHP fatal error in 5.6, but works fine in 7.0 and above. There is no documented change to isset that I could find stating that it works with arrays as constants.

<?php
class Test
{
    const A = [1];
}

echo isset(Test::A[0]);

Does anyone know of any documentation stating this was an actual change? Is it safe to use isset with arrays as constants?

PHP 5.6.30 error:

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

like image 759
Ben Guest Avatar asked Apr 20 '17 12:04

Ben Guest


1 Answers

isset() is a language construct and not a function, so perhaps this paragraph (from http://php.net/manual/en/migration70.incompatible.php) applies:

PHP 7 now uses an abstract syntax tree when parsing source files. This has permitted many improvements to the language which were previously impossible due to limitations in the parser used in earlier versions of PHP, but has resulted in the removal of a few special cases for consistency reasons, which has resulted in backward compatibility breaks.

like image 50
CXJ Avatar answered Nov 20 '22 10:11

CXJ