You want handler functions to execute whenever you read and write object properties. This lets you write generalized code to handle property access in your class.
Use the magical methods __get( ) and __set( ) to intercept property requests.
To improve this abstraction, also implement __isset( ) and __unset( ) methods to make the class behave correctly when you check a property using isset( ) or delete it using unset( ).
Property overloading allows you to seamlessly obscure from the user the actual location of your object's properties and the data structure you use to store them.
For example, the Person class shown in Example 7-30 stores variables in an array, $__data.
Implementing magic accessor methods
Example 7-31 shows how to use the Person class.
Using magic accessor methods
When you set data, __set( ) rewrites the element inside of $__data. Likewise, use __get( ) to trap the call and return the correct array element.
Using these methods and an array as the alternate variable storage source makes it less painful to implement object encapsulation. Instead of writing a pair of accessor methods for every class property, you use __get( ) and __set( ).
With __get( ) and __set( ), you can use what appear to be public properties, such as $johnwood->name, without violating encapsulation. This is because the programmer isn't reading from and writing to those properties directly, but is instead being routed through accessor methods.
The __get( ) method takes the property name as its single parameter. Within the method, you check to see whether that property has a value inside $__data. If it does, the method returns that value; otherwise, it returns false.
The __set( ) method takes two arguments: the property name and the new value. Otherwise, the logic inside the method is similar to __get( ).
Besides reducing the number of methods in your classes, these magical methods also make it easy to implement a centralized set of input and output validation.
Additionally, Example 7-32 shows how you can also enforce exactly what properties are legal and illegal for a given class.
Enforcing property access using magic accessor methods
In this updated version of the code, you explicitly list the object's valid properties names when you define the $__data property. Then, inside __set( ), you use isset( ) to confirm that all property writes are going to allowable names.
Preventing rogue reads and writes is why the visibility of the $__data property isn't public, but protected. Otherwise, someone could do this:
$person = new Person; $person->__data['fake_property'] = 'fake_data';
because the magical accessors aren't used for existing properties.
Pay attention to this important implementation detail. In particular, if you're expecting people to extend the class, they could introduce a property that conflicts with a property you're expecting to handle using __get( ) and __set( ). For that reason, the property in Example 7-32 is called $__data with two leading underscores.
You should consider prefixing all your "actual" properties in classes where you use magical accessors to prevent collisions between properties that should be handled using normal methods and ones that should be routed through __get( ) and __set( ).
There are three downsides to using __get( ) and __set( ). First, these methods only catch missing properties. If you define a property for your class, __get( ) and __set( ) are not invoked by PHP when that property is accessed.
This is the case even if the property you're trying to access isn't visible in the current scope (for instance, when you're reading a property that exists in the class but isn't accessible to you, because it's declared private). Doing this causes PHP to emit a fatal error:
PHP Fatal error: Cannot access private property...
Second, these methods completely destroy any notion of property inheritance. If a parent object has a __get( ) method and you implement your own version of __get( ) in the child, your object won't function correctly because the parent's __get( ) method is never called.
You can work around this by calling parent::__get( ), but it is something you need to explicitly manage instead of "getting for free" as part of OO design.
The illusion is incomplete because it doesn't extend to the isset( ) and unset( ) methods. For instance, if you try to check if an overloaded property isset( ), you will not get an accurate answer, as PHP doesn't know to invoke __get( ).
You can fix this by implementing your own version of these methods in the class, called __isset( ) and __unset( ), shown in Example 7-33.
Implementing magic methods for isset( ) and unset( )
The __isset( ) method checks inside the $data element and returns true or false depending on the status of the property you're checking.
Likewise, __unset( ) passes back the value of unset( ) applied to the "real" property, or false if it's not set.
Implementing these two methods isn't required when using __get( ) and __set( ), but it's best to do so as it's hard to predict how you may use object properties. Failing to code these methods will lead to confusion when someone (perhaps even yourself) doesn't know (or forgets) that this class is using magic accessor methods.
However, the __isset( ) and __unset( ) methods are only available as of PHP 5.1.
Other reasons to consider not using magical accessors are:
7.11.4. See Also
Documentation on magic methods found at http://www.php.net/manual/en/language.oop5.magic.php.