Magic __get getter for static properties in PHP

public static function __get($value) 

doesn't work, and even if it is, it happens that I already need a magic __get getter, for example, properties in the same class.

Perhaps this is a yes or no question, so is that possible?

+49
oop properties php getter
Aug 14 '09 at 18:24
source share
5 answers

No, It is Immpossible.

Quote __get man page :

Member overloading only works in the context of an object. These magic methods will not work in a static context. Therefore, these methods may not be declared static.


In PHP 5.3, __callStatic added; but there are no __getStatic and __setStatic ; even if the idea of ​​having / coding them often comes back to php internals @ mailling-list.

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

+53
Aug 14 '09 at 18:27
source share

Perhaps someone else needs this:

 static public function __callStatic($method, $args) { if (preg_match('/^([gs]et)([AZ])(.*)$/', $method, $match)) { $reflector = new \ReflectionClass(__CLASS__); $property = strtolower($match[2]). $match[3]; if ($reflector->hasProperty($property)) { $property = $reflector->getProperty($property); switch($match[1]) { case 'get': return $property->getValue(); case 'set': return $property->setValue($args[0]); } } else throw new InvalidArgumentException("Property {$property} doesn't exist"); } } 
+16
Sep 13 '11 at 18:56
source share

Very nice mbzuchalski. But it seems to work only with public variables. Just change your switch to this so that it can access private / secure:

 switch($match[1]) { case 'get': return self::${$property->name}; case 'set': return self::${$property->name} = $args[0]; } 

And you probably want to modify the if to limit the available variables, otherwise it will result in them being closed or protected.

 if ($reflector->hasProperty($property) && in_array($property, array("allowedBVariable1", "allowedVariable2"))) {...) 

So, for example, I have a class designed to output various data for me from a remote server using the ssh pear module, and I want it to make certain assumptions about the target directory based on which server it was asked to look at. A thin version of the mbrzuchalski method is ideal for this.

 static public function __callStatic($method, $args) { if (preg_match('/^([gs]et)([AZ])(.*)$/', $method, $match)) { $reflector = new \ReflectionClass(__CLASS__); $property = strtolower($match[2]). $match[3]; if ($reflector->hasProperty($property)) { if ($property == "server") { $property = $reflector->getProperty($property); switch($match[1]) { case 'set': self::${$property->name} = $args[0]; if ($args[0] == "server1") self::$targetDir = "/mnt/source/"; elseif($args[0] == "server2") self::$targetDir = "/source/"; else self::$targetDir = "/"; case 'get': return self::${$property->name}; } } else throw new InvalidArgumentException("Property {$property} is not publicly accessible."); } else throw new InvalidArgumentException("Property {$property} doesn't exist."); } } 
+4
Mar 11 '14 at 2:26
source share

In addition, you can get static properties by accessing them as properties of an element using __get ():

 class ClassName { static $data = 'smth'; function __get($field){ if (isset($this->$field)){ return $this->$field; } if(isset(self::$$field)){ return self::$$field; // here you can get value of static property } return NULL; } } $obj = new ClassName(); echo $obj->data; // "smth" 
0
Jul 11 '16 at 10:56
source share

try the following:

 class nameClass{ private static $_sData = []; private static $object = null; private $_oData = []; public function __construct($data=[]){ $this->_oData = $data; } public static function setData($data=[]){ self::$_sData = $data; } public static function Data(){ if( empty( self::$object ) ){ self::$object = new self( self::$_sData ); } return self::$object; } public function __get($key) { if( isset($this->_oData[$key] ){ return $this->_oData[$key]; } } public function __set($key, $value) { $this->_oData[$key] = $value; } } nameClass::setData([ 'data1'=>'val1', 'data2'=>'val2', 'data3'=>'val3', 'datan'=>'valn' ]); nameClass::Data()->data1 = 'newValue'; echo(nameClass::Data()->data1); echo(nameClass::Data()->data2); 
0
Oct 01 '17 at 15:07 on
source share



All Articles