Here's the documentation from static
keyword PHP.net:
A property declared as static cannot be accessed with an instantiated class object (though a static method can).
So why the following code works?
Here's their example code (I've shorten it):
<?php
class Foo
{
public static $my_static = 'foo';
}
$foo= new Foo();
print $foo::$my_static; //print 'foo'
?>
Why $foo::$my_static still
works here? Thank you everybody!
A ::
(double colon, or T_PAAMAYIM_NEKUDOTAYIM
as the PHP parser calls it) is termed the scope resolution operator for a reason. It resolves the access to a static property on an object reference.
This appears to have been impossible before PHP 7, however an RFC was issued to address the behavior and later implemented in the language. The PHP documentation can sometimes be slow to update and include new features, and therefore mislead unless you also follow the RFC process at wiki.php.net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With