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

StackOverflow https://stackoverflow.com/questions/21265540

  •  30-09-2022
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

You are updating country on stdClass while you have defined __set method for Person. Updating stdClass has nothing to do with invoking __set method for Person class.

If a method does not exist in your class (or is private in your case), PHP calls __set in case you defined a generic catch all setter.

I'm willing to bet that the code:

$customer->home_address->country = 'USA';

Breaks down internally as:

$temp = $customer->__get('home_address');
$temp->country = 'USA';

Therefore the internal state of the $customer object doesn't change. I think you'll need to do something like:

$address = $customer->home_address;
$address->country = 'USA';
$customer->home_address = $address;

Or, if you really need to set/update properties like this, why not make it a public property and not worry about __get() and __set()?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top