Accessing PHP Class Constants

前端 未结 4 1058
谎友^
谎友^ 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条回答
  •  余生分开走
    2020-12-14 19:48

    If you are within the class, you can access the constant like this:

    self::MY_CONSTANT;
    

    For example:

    class MyClass {
        const MY_CONSTANT = 'constant value';
    
        public function showConstant() {
            echo self::MY_CONSTANT;
        }
    }
    

    http://php.net/manual/en/language.oop5.constants.php

提交回复
热议问题