Search code examples
phpoopmagic-methods

Why doesn't the PHP __set() magic method fire when updating a property of an object stored as a private property?


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?


Solution

  • I don't think you are looking at this correctly.

    When you try to set $customer->home_address->country, you are trying to set a property on the stdClass() object that is set to home_addresss property of $customer. This would in no way invoke a setter on $customer.

    Your initial setting of home_address does in invoke the __set() magic method as home_address is not defined as a property on Person class.