Why cannot the value assignment be a constant during array initialization? -Php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.