I am completely baffled by this one and hope one of my techie friends can help.

I’m using a PHP class with magic methods to set and get the properties of that class.    The idea is to use private properties in the class so that the PHP magic methods can take over and determine whether to update a WordPress user meta entry, blog entry, or standard option based on which proper of the class is being retrieved or stored.

Here is a sample psuedo-class based on a real-world implementation.  It is simplified so there may be syntax issues.

The problem in the real-world code is that this call:

Does NOT call the __set() magic method for the account_stats property.

It DOES call the __set() magic method for the status property.

According to PHP any attempt to set an “inaccessible property” will fire the __set() magic method.     OK, so a private property is available to the methods of the class which, in my mind, means BOTH the account_stats AND the status properties are accessible and would NOT call the __set() magic method.     Yet the magic method IS called for the $this->status = $state line.

The patch it to call the setter directly in set_account:

Does anyone know a PHP guru out there that might be able to explain this one?      Why is the private property status handled differently than the private property account_status?    The order of the statements in the set_account() method do not make a difference.    Using $this->status always fires the __set() magic method but $this->account_stats does not.


Share Your Insight

%d bloggers like this: