Reputation: 5052
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.
Upvotes: 1
Views: 48
Reputation: 316969
Assuming you meant
Class Foo{
public static $StaticVariable = 42;
}
class Bar {
private $ClassInstance;
public function fn() {
$this->ClassInstance = new Foo();
return $this->ClassInstance::$StaticVariable;
}
}
$bar = new Bar;
echo $bar->fn();
Then this works as of PHP7. Prior to that, the parser could simply not dereference this. See the wiki page linked in the comments to your question for details.
You could use get_class($this->ClassInstance)::$StaticVariable;
though.
Upvotes: 3
Reputation: 1430
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
Upvotes: 2
Reputation: 1317
You can access it with self::$staticVariable
or foo::$staticVariable
. No need to make use of the instance.
Upvotes: 2