Recipe 7.13. Aggregating Objects


7.13.1. Problem

You want to compose two or more objects together so that they appear to behave as a single object.

7.13.2. Solution

Aggregate the objects together and use the __call( ) magic method to intercept method invocations and route them accordingly:

class Address {     protected $city;     public function setCity($city) {         $this->city = $city;     }     public function getCity() {         return $this->city;     } } class Person {     protected $name;     protected $address;     public function __construct() {         $this->address = new Address;     }     public function setName($name) {         $this->name = $name;     }     public function getName() {         return $this->name;     }     public function __call($method, $arguments) {         if (method_exists($this->address, $method)) {             return call_user_func_array(                 array($this->address, $method), $arguments);         }     } } $rasmus = new Person; $rasmus->setName('Rasmus Lerdorf'); $rasmus->setCity('Sunnyvale'); print $rasmus->getName() . ' lives in ' . $rasmus->getCity() . '.'; Rasmus Lerdorf lives in Sunnyvale.

An instance of the Address object is created during the construction of every Person. When you invoke methods not defined in Person, the __call( ) method catches them and, when applicable, dispatches them using call_user_func_array( ).

7.13.3. Discussion

In this recipe, you cannot say a Person "is an" Address or vice versa. Therefore, it doesn't make sense for one class to extend the other.

However, it makes sense for them to be separate classes so that they provide maximum flexibility and reuse, as well as reduced duplicated code. So you check if another rule'the "has a" rule'applies. Since a Person "has an" Address, it makes sense to aggregate the classes together.

With aggregation, one object acts as a container for one or more additional objects. This is another way of solving the problem of multiple inheritance because you can easily piece together an object out of smaller components.

For example, a Person object can contain an Address object. Clearly, People have addresses. However, addresses aren't unique to people; they also belong to businesses and other entities. Therefore, instead of hardcoding address information inside of Person, it makes sense to create a separate Address class that can be used by multiple classes.

Example 7-34 shows how this works in practice.

Aggregating an address object

class Address {     protected $city;     public function setCity($city) {         $this->city = $city;     }     public function getCity() {         return $this->city;     } } class Person {     protected $name;     protected $address;     public function __construct() {         $this->address = new Address;     }     public function setName($name) {         $this->name = $name;     }     public function getName() {         return $this->name;     }     public function __call($method, $arguments) {         if (method_exists($this->address, $method)) {             return call_user_func_array(                 array($this->address, $method), $arguments);         }     } }

The Address class stores a city and has two accessor methods to manipulate the data, setCity( ) and getCity( ).

Person has setName( ) and getName( ), similar to Address, but it also has two other methods: __construct( ) and __call( ).

Its constructor instantiates an Address object and stores it in a protected $address property. This allows methods inside Person to access $address, but prevents others from talking directly to the class.

Ideally, when you call a method that exists in Address, PHP would automatically execute it. This does not occur, since Person does not extend Address. You must write code to glue these calls to the appropriate methods yourself.

Wrapper methods are one option. For example:

public function setCity($city) {         $this->address->setCity($city); }

This setCity( ) method passes along its data to the setCity( ) method stored in $address. This is simple, but it is also tedious because you must write a wrapper for every method.

Using __call( ) lets you automate this process by centralizing these methods into a single place, as shown in Example 7-35.

Centralizing method invocation using __call( )

public function __call($method, $arguments) {         if (method_exists($this->address, $method)) {             return call_user_func_array(                 array($this->address, $method), $arguments);         } }

The __call( ) method captures any calls to undefined methods in a class. It is invoked with two arguments: the name of the method and an array holding the parameters passed to the method. The first argument lets you see which method was called, so you can determine whether it's appropriate to dispatch it to $address.

Here, you want to pass along the method if it's a valid method of the Address class. Check this using method_exists( ) , providing the object as the first parameter and the method name as the second.

If the function returns TRue, you know this method is valid, so you can call it. Unfortunately, you're still left with the burden of unwrapping the arguments out of the $arguments array. That can be painful.

The seldom used and oddly named call_user_func_array( ) function solves this problem. This function lets you call a user function and pass along arguments in an array. Its first parameter is your function name, and the second is the array of arguments.

In this case, however, you want to call an object method instead of a function. There's a special syntax to cover this situation. Instead of passing the function name, you pass an array with two elements. The first element is the object, and the other is the method name.

This causes call_user_func_array( ) to invoke the method on your object. You must then return the result of call_user_func_array( ) back to the original caller, or your return values will be silently discarded.

Here's an example of Person that calls both a method defined in Person and one from Address:

$rasmus = new Person; $rasmus->setName('Rasmus Lerdorf'); $rasmus->setCity('Sunnyvale'); print $rasmus->getName() . ' lives in ' . $rasmus->getCity() . '.'; Rasmus Lerdorf lives in Sunnyvale.

Even though setCity( ) and getCity( ) aren't methods of Person, you have aggregated them into that class.

You can aggregate additional objects into a single class, and also be more selective as to which methods you expose to the outside user. This requires some basic filtering based on the method name.

7.13.4. See Also

Documentation on magic methods at http://www.php.net/manual/en/language.oop5.magic.php.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net