Article

The PHP Anthology Volume 1, Chapter 2 - Object Oriented PHP

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

How do objects interact?

Aside from inheritance, there are other ways for objects to interact; for example, one object uses another object. In many ways, such interactions are more important than inheritance, and this is where the object oriented paradigm shows its real power.

There are two ways in which one object can use another: aggregation and composition.

Aggregation

Aggregation occurs when one object is given another object on “temporary loan.” The second object will usually be passed to the first through one of the first’s member functions. The first object is then able to call methods in the second, allowing it to use the functionality stored in the second object for its own purposes.

A common example of aggregation in action involves a database connection class. Imagine you pass a database connection class to some other class, which then uses the database connection class to perform a query. The class performing the query aggregates the database connection class.

Here’s a simple example using the MySQL class, which we’ll create in Chapter 3, PHP and MySQL:

Example 2.23. 13.php

<?php          
// Include the MySQL database connection class          
require_once 'Database/MySQL.php';          
         
// A class which aggregates the MySQL class          
class Articles {          
 var $db;          
 var $result;          
 // Accept an instance of the MySQL class          
 function Articles(&$db)          
 {          
   // Assign the object to a local member variable          
   $this->db = &$db;          
   $this->readArticles();          
 }          
 function readArticles()          
 {          
   // Perform a query using the MySQL class          
   $sql = "SELECT * FROM articles LIMIT 0,5";          
   $this->result = &$this->db->query($sql);          
 }          
 function fetch()          
 {          
   return $this->result->fetch();          
 }          
}          
         
// Create an instance of the MySQL class          
$db = &new MySQL('localhost', 'harryf', 'secret', 'sitepoint');          
         
// Create an instance of the Article class, passing it the MySQL          
// object          
$articles = &new Articles($db);          
         
while ($row = $articles->fetch()) {          
 echo '<pre>';          
 print_r($row);          
 echo '</pre>';          
}          
?>

In the above example, we instantiate the MySQL class outside the Articles class, then pass it to the Articles constructor as Articles is instantiated. Articles is then able to use the MySQL object to perform a specific query. In this case, Articles aggregates the MySQL object. Figure 2.2 illustrates this relationship with UML.

1264_aggregation
Figure 2.2. Aggregation

Composition

Composition occurs when one object “completely owns” another object. That is, the first object was responsible for creating (instantiating) the second object. There are many cases in which this can be useful, although, typically, composition is used when it’s likely that the first object will be the only one that needs to use the second object.

One example from Chapter 1, Access Control is the Auth class, which composes an instance of the Session class, creating it in the constructor:

class Auth {          
 …          
         
 /**          
  * Instance of Session class          
  * @var Session          
  */          
 var $session;          
         
 …          
         
 function Auth (&$dbConn, $redirect, $md5 = true)          
 {          
   $this->dbConn = &$dbConn;          
   $this->redirect = $redirect;          
   $this->md5 = $md5;          
   $this->session = &new Session();          
   $this->checkAddress();          
   $this->login();          
 }

Because the Auth class needs to read and write to session variables, and only a limited number of other, unrelated classes in an application are likely also to need to use Session, it’s logical that it gets to create its own Session object.

Figure 2.3 illustrates the composition in this example with UML.

1264_composition
Figure 2.3. Composition

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links