Assume we have this code:
class SomeClass{
private $somePrivateField;
public function __get($name){
$function = "get".ucfirst($name);
return $function;
}
public function __set($name,$value){
$function = "set".ucfirst($name);
return $function($value);
}
public function __call($name, $arguments) {
//if function being called is getSomething
//getThat private/protected field if exists and return
//if not raise exception
// similar for setSomething...
}
}
This is a passage from some tutorial:
The __get() method accepts an argument that represents the name of the property being set. In the case of $obj->property, the argument will be property.
Our __get() method then converts this to getProperty, which matches the pattern we defined in the __call() method. What this means is that $obj->property will first try to set a public property with the same name, then go to __get(), then try to call the public method setProperty(), then go to __call(), and finally set the protected $_property.
So when I say somewhere in my code
$obj->property
I can understand it tried to access the public field first.. Why does it go to __get() first? Why not __set() ? Why does it go to __set() then?
Can someone please explain? Thanks...