Accessing PHP Class Constants

前端 未结 4 1054
谎友^
谎友^ 2020-12-14 19:14

The PHP manual says

Like static members, constant values can not be accessed from an instance of the object.

which explains wh

4条回答
  •  猫巷女王i
    2020-12-14 19:44

    To quote the manual:

    As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

    It goes on to use this example:

    $class = new MyClass();
    echo $class::constant."\n"; // As of PHP 5.3.0
    

    So $inst::someconstant is supposed to work.

    As to why $this->inst::someconstant gives a parsing error, I don't know. PHP is funny about some things.

提交回复
热议问题