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
.
__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.
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.
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