Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magic __get getter for static properties in PHP

public static function __get($value) 

does not work, and even if it did, it so happens that I already need the magic __get getter for instance properties in the same class.

This probably is a yes or no question, so, it is possible?

like image 758
treznik Avatar asked Aug 14 '09 18:08

treznik


People also ask

How can I get static properties in PHP?

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 .

What is __ get in PHP?

PHP calls the __get() method automatically when you access a non-existing or inaccessible property. PHP calls the __set() method automatically when you assign a value to a non-existing or inaccessible property.

What is the magic method in PHP?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

What is __ Tostring in PHP?

The __toString() function returns the string content of an element. This function returns the string content that is directly in the element - not the string content that is inside this element's children!


1 Answers

No, it is not possible.

Quoting the manual page of __get :

Member overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods can not be declared static.


In PHP 5.3, __callStatic has been added ; but there is no __getStatic nor __setStatic yet ; even if the idea of having/coding them often comes back on the php internals@ mailling-list.

There is even a Request for Comments: Static classes for PHP
But, still, not implemented (yet ? )

like image 116
Pascal MARTIN Avatar answered Oct 06 '22 11:10

Pascal MARTIN