Why cannot the value assignment be a constant during array initialization? A strange phenomenon just found
In the following array, I set it to the class attribute, and SYSTEM_LIB is the constant defined by me.
final class Application { public static $_lib=array( 'route' => SYSTEM_LIB.'/lib_route.php', 'mysql' => SYSTEM_LIB.'/lib_mysql.php', );}
However, the final running error is "Parse error: syntax error, unexpected ".
When I put the array into the method, it can be created normally.
final class Application { public static $_lib=array(); public static function run(){ self::$_lib =array( 'route' => SYSTEM_LIB.'/lib_route.php', 'mysql' => SYSTEM_LIB.'/lib_mysql.php', ); }}
When an array is set as a class attribute, the values in the array must be enclosed in quotation marks, and the array does not recognize constants.
Reply to discussion (solution)
When defining a class, attributes cannot be assigned uncertain values.
User constants are defined when the program is running.
The syntax check is performed before the program runs.
The system constant is a definite value.
class T { var $os = PHP_OS;}$p = new T;echo $p->os;
Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed.
Like static variables in PHP, static attributes can only be initialized with one nominal value or constant (versions earlier than PHP5.6); expressions are not allowed.
In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.
PHP5.6 and later versions are the same as the constant expression rules. some specific expressions can be used as long as they can be calculated during compilation.
Therefore, you can run PHP 5.6 or later normally.
Static variables cannot be initialized using other variables or functions as values.