Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP __get and __set magic methods

Unless I'm completely mistaken, the __get and __set methods are supposed to allow overloading of the → get and set.

For example, the following statements should invoke the __get method:

echo $foo->bar; $var = $foo->bar; 

And the following should use the __set method:

$foo->bar = 'test'; 

This was not working in my code, and is reproducible with this simple example:

class foo {      public $bar;     public function __get($name) {          echo "Get:$name";         return $this->$name;     }      public function __set($name, $value) {          echo "Set:$name to $value";         $this->$name = $value;     } }   $foo = new foo();  echo $foo->bar; $foo->bar = 'test';  echo "[$foo->bar]"; 

This only results in:

[test] 

Putting some die() calls in there shows that it is not hitting it at all.

For now, I just said screw it, and am manually using __get where it's needed for now, but that's not very dynamic and requires knowledge that the 'overloaded' code is in fact not being called unless specifically called. I'd like to know if this is either not supposed to function the way I've understood that it should or why this is not working.

This is running on php 5.3.3.

like image 606
airbear Avatar asked Jan 17 '11 13:01

airbear


2 Answers

__get, __set, __call and __callStatic are invoked when the method or property is inaccessible. Your $bar is public and therefor not inaccessible.

See the section on Property Overloading in the manual:

  • __set() is run when writing data to inaccessible properties.
  • __get() is utilized for reading data from inaccessible properties.

The magic methods are not substitutes for getters and setters. They just allow you to handle method calls or property access that would otherwise result in an error. As such, there are much more related to error handling. Also note that they are considerably slower than using proper getter and setter or direct method calls.

like image 103
Gordon Avatar answered Sep 19 '22 17:09

Gordon


I'd recommend to use an array for storing all values via __set().

class foo {      protected $values = array();      public function __get( $key )     {         return $this->values[ $key ];     }      public function __set( $key, $value )     {         $this->values[ $key ] = $value;     }  } 

This way you make sure, that you can't access the variables in another way (note that $values is protected), to avoid collisions.

like image 29
Fidi Avatar answered Sep 20 '22 17:09

Fidi