Gives an Error:
$this->model::byUserPermission()
Leads to: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
Works:
$facade = $this->model;
$facade::byUserPermission()
Is this a PHP Bug? Or can someone explain this to my, why that is happening (I am using php 5.6 and i am new to php. From my point of view, both are exactly the same). Thanks
Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ). print $foo::$my_static .
The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method.
PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.
The problem is that this statement $this->model::byUserPermission()
is ambiguous. And can be interpreted in multiple ways.
1) You could be trying to use the model
property of the class that you are in to call a class's static method. As you are attempting in your question.
2) You could also mean you want to access the property of the class returned by the static function byUserPermission()
in the model
class.
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