Given the following design pattern...
class Person
{
private $properties = array(); // will eventually hold an address object
public function __set($name, $value)
{
$this->properties[$name] = $value;
}
public function __get($name)
{
if (!empty($this->properties[$name])) {
return $this->properties[$name];
}
}
}
// create a generic object to hold address data
$home_address = new stdClass();
$home_address->postal = '12345';
// instantiate a new person object and assign $home_address as property
$customer = new Person;
// __set() magic method fires just fine
$customer->home_address = $home_address;
// now try to set a property of the address object
// __set() magic method DOES NOT fire
$customer->home_address->country = 'USA';
// best work around i can find is to assign the current
// address to a temporary variable and then re-assign
// it back to the property
$temp_address = $customer->home_address;
$temp_address->country = 'USA';
// __set() magic method fires just fine
$customer->home_address = $temp_address;
Why doesn't the __set()
method fire when I add / update a property (for example, country
) on the private property of home_address
directly?
The only work around I have found is to use a temporary variable and completely overwrite the address
private property in order to fire the __set()
method.
Any best practice advice or am I not understanding the best way to use the __set() method here?