dphfwzn8269 2017-03-09 21:33
浏览 58

从类实例访问静态变量

I would like to use this line of code:

$this->ClassInstance::$StaticVariable;

Where this is the basic setup:

Class foo{
   public static $StaticVariable;
}

$this->ClassInstance = new foo();

What is the reasoning behind this being a parse error?

When this:

$ClassInstance = $this->ClassInstance;
$ClassInstance::$StaticVariable;

Is perfectly valid.

  • 写回答

3条回答 默认 最新

  • du9843 2017-03-09 21:37
    关注

    In PHP docs

    Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

    source http://php.net/manual/en/language.oop5.static.php

    评论

报告相同问题?