Referring to a static member of a subclass

2019-02-27 10:34发布

问题:

I wish there was a sub keyword that would make the code below print value on execution. There isn't though and I wonder - is there an existing way to refer to a member of a subclass?

class Main
{
    static function foo()
    {
        echo sub::$variable;
    }
}

class Sub extends Main
{
    static $variable = "value";
}

Sub::foo();

回答1:

I think you are looking for static::$variable. That's called Late Static Binding and is available as of PHP 5.3.



标签: php oop