Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why static properties are not accessible by "->" but accessible by "::"?

Tags:

php

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!

like image 840
Best_Name Avatar asked Nov 08 '22 19:11

Best_Name


1 Answers

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.

like image 129
Kubo2 Avatar answered Nov 14 '22 21:11

Kubo2