网站建设的网站,哈尔滨微网站建设公司哪家好,做什么网站能吸引流量,沈阳seo推广刚刚发现的一个很奇怪的现象下面这个数组我设置为类的属性#xff0c;其中SYSTEM_LIB为我定义的常量final class Application { public static $_libarray( route SYSTEM_LIB./lib_route.php, mysql SYSTEM_LIB./lib_mysql.php, );}但最终运行时报错为Parse error: …刚刚发现的一个很奇怪的现象下面这个数组我设置为类的属性其中SYSTEM_LIB为我定义的常量final class Application { public static $_libarray( route SYSTEM_LIB./lib_route.php, mysql SYSTEM_LIB./lib_mysql.php, );}但最终运行时报错为Parse error: syntax error, unexpected而我将该数组放入方法中时就能正常创建final class Application { public static $_libarray(); public static function run(){ self::$_lib array( route SYSTEM_LIB./lib_route.php, mysql SYSTEM_LIB./lib_mysql.php, ); }}我分析是因为当数组被设置为类属性时数组内的值必须在引号内数组不认识常量回复讨论(解决方案)类定义时属性不能赋予不确定的值用户常量是在程序运行时定义的而语法检查是在程序运行前进行的而系统常量就是确定的值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.与PHP的静态变量一样静态属性只能使用一个字面值或常量来初始化(PHP5.6之前的版本);表达式是不允许的。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以上版本则与常量表达式的规则一样可以使用一些特定的表达式只要其能在编译时被计算所以你的第一种写法在PHP5.6以上版本是可以正常运行的。印象中static静态变量不能用其他变量或函数作为值初始化本文原创发布php中文网转载请注明出处感谢您的尊重