Are there any potential pitfalls to using
$this->{$name}
instead of
$this->some_array[$name]
in my class getters and setters?
To clarify, I mean WITHIN the __get and __set methods, not while calling them.
For example:
function __get($name) {
...
return $this->{$name};
}
A getter/setter will only be invoked if the property is not accessible directly. When dynamically setting properties that are not defined in the class itself, the property will be created with
publicvisibility. Which means it’s accessible from anywhere, and the getters/setters will not be invoked when accessing it.To illustrate, the below outputs “setting bar” only once:
That’s probably not something you want. Furthermore, it allows anybody to change any property of your object, which is probably also not what you want.