Object-Oriented PHP Part 2: Relationships

Posted Monday 21st August, 2006

Following on from my posts ”Object-Oriented Concepts” and ”Object-Oriented Javascript”, I’m going to take a look at OOP in PHP.

In ”Part 1: Definition” we took a look at defining objects and classes in PHP. In part 2 I’m going to look at the most important part of any object-oriented system - the relationships.


The relationships you use dictate the way objects interact with each other. Here’s how to model them in PHP.


Inheritance allows us to define a class that inherits all the attributes and methods of another class. To do this in PHP4 and PHP5, we need to employ the extends keyword. Here’s an example based around HTML form elements:

class formElement
  var $str_id;
  var $str_name;
  var $str_class;

  function isRequired()
    // Validation code here

class dateInput extends formElement
  var $str_value;

  // Specific validation for dateInput
  function isValidDate()
    // Validation code here

In the above example, the class dateInput is inheriting all the attributes and methods of the class formElement. This means we could do all of the following:

// Instantiate our class as an object
$myDate = new dateInput();

$myDate->str_id = 'mydate';
$myDate->str_name = 'mydate';
$myDate->str_value = '2006/06/09';

Some languages allow multiple inheritance; allowing you to inherit from more than one class. Unfortunately, this is not the case in PHP.

If your parent class has a constructor, PHP won’t evaluate it when you instantiate the child class. If you wish the constructor to be run, you’ll need to call in as if it were any other method within the class. Here’s an example of what I mean:


class fruit
  // Constructor
  function fruit()

class apple extends fruit
  function apple()
    // Call the constructor of our parent class


class fruit
  // Constructor
  function __construct()

class apple extends fruit
  function __construct()
    // Call the constructor of our parent class
    // using the scope resolution operator -
    // we'll talk about this later.

In the PHP5 example you’ll notice we don’t use the $this keyword. This is simply because $this could evaluate to either apple or fruit because the constructor method for each is called __construct. To get around this we use the scope resolution operator which I will talk about a little later on.

Composition - Association and Aggregation

Association and aggregation differ from inheritance because rather than passing the methods and properties of a class to another class, they involve instantiating whole objects and using them as properties, or passed-in parameters, in another class.

This might sound complicated but it’s really very simple in practice. I’m going to borrow some of the examples in my Object-Oriented Concepts post and adapt them for PHP:


class brick
  var $sample_attribute;

class wall
  var $brick1;
  var $brick2;
  var $brick3;
  var $brick4;

  // Constructor
  function wall()
    $this->brick1 = new brick();
    $this->brick2 = new brick();
    $this->brick3 = new brick();
    $this->brick4 = new brick();

In the above example we have a class wall which has four brick attributes. To associate our brick class we instantiate it as the $brick attributes of the wall class.

We can now access the $sample_attribute of each brick within our wall class like so:

echo $this->brick1->sample_attribute;
echo $this->brick2->sample_attribute;
echo $this->brick3->sample_attribute;
echo $this->brick4->sample_attribute;

Or from outside an instantiated object like so:

$myWall = new wall();

echo $myWall->brick1->sample_attribute;
echo $myWall->brick2->sample_attribute;
echo $myWall->brick3->sample_attribute;
echo $myWall->brick4->sample_attribute;

As you can see, this type of relationship has the potential to be highly useful - however, if you wish to share an object between other classes (for instance, a database connection object) we run into a problem. In our example you can see that the brick objects are encapsulated within our wall class which means they are only accessible within our myWall object. To get around this problem we could use aggregation.

Aggregation basically involves passing an instantiated object into a class so that the class can “use” our object. Once again, I’ll poach one of my earlier examples to illustrate what I mean. To save on code snippets, I’m only going to use PHP4 syntax for the moment:


class person

class car
  var $driver;

  // Constructor
  function car(&$driver)
    $this->driver = (object) $driver;

// Instantiate our person object
$me = new person();

// Now pass it in as $driver
$myMotor = new car($me);

In this example we instantiate our class person as an object ($me). We then pass the $me object into the car class so that it is used as the $driver attribute in the $myMotor object. If we were to destroy the $myMotor object, we wouldn’t destroy the $me object; which means we could carry on using it for other things. In fact, even though we’re using the $me object within our $myMotor object, we could still use it in other objects like so:

class van
  var $driver;

  // Constructor
  function van(&$driver)
    $this->driver = (object) $driver;

// Our $me object already exists
// so pass it in as $driver
$myVan = new van($me);

If you look at our example you’ll notice the ampersand symbol (&) before the parameter declaration of our constructor method. This tells PHP that the parameter is passed by reference. This is the key to the aggregation relationship.


PHP references allow you to define two variables that refer to the same content. To do this we use the & operator. Here’s an example:

$a =& $b;

In this example $a and $b point to the same content.

The same syntax can be used with functions, that return references, and with the new operator:

$bar =& new fooclass();
$foo =& find_var($bar);

If we don’t use the & operator when we’re instantiating an object, PHP creates a copy of said object. If you use $this in the class it will operate on the current instance of the class. The assignment without & will copy the instance (i.e. the object) and $this will operate on the copy, which is not always what is desired. Usually you want to have a single instance to work with, due to performance and memory consumption issues.

Basically, it’s a good idea to always assign a reference when instantiating an object unless you have a specific case not to.

Note: Since PHP5, in fact, new returns references automatically so using =& in this context is deprecated and produces E_STRICT level messages.

There is another way that references help us when developing object-oriented solutions in PHP; by allowing method parameters to be passed by reference as we did in our aggregation example above.

Passing by reference basically allows us to use or modify a variable within a method (function). Here’s a good example from the PHP documentation:

function foo(&$var)

// $a is 6 here

In the aggregation example we’re passing an entire object as a parameter. If we passed the parameter without the reference we would be creating a copy of the object that was only available within the function. By adding the reference we’re simply telling the function that the parameter is another name for our original object.


Although these relationships can appear simple to begin with, it’s all too easy to overlook their hidden power. Used in the right way, relationships can help to build highly powerful, easily extendable applications with relatively little fuss.

Deciding which relationship to use can be tricky; and it’s certainly not something you’ll get right everytime. However, rest assured, if you experiment and use them regularly, you’ll find it easier to solve problems that are presented to you.

In the next post, I’ll be taking a look at extending our relationships a little further using visibility and polymorphism.

Click here to continue on to “Part 3: Taking Relationships Further”…

Included in: Development, PHP, Web


  1. Accessibility
  2. Agile
  3. Ajax
  4. Apache
  5. API
  6. Architecture
  7. Books
  8. Browsers
  9. CMS
  10. CouchDB
  11. CSS
  12. Design
  13. Development
  14. Django
  15. Email
  16. Events
  17. Gaming
  18. Grammar
  19. Hardware
  20. HTML
  21. HTTP
  22. Humour
  23. Idea
  24. Information Architecture
  25. JavaScript
  26. jQuery
  27. Lean
  28. Life
  29. Linux
  30. Literature
  31. Mac OS X
  32. Management
  33. Meme
  34. Microformats
  35. Monday
  36. MySQL
  37. Networking
  38. News
  39. Personal
  40. Photoshop
  41. PHP
  42. Process
  43. Python
  44. Reference
  45. REST
  46. Science
  47. SEO
  48. Server
  49. Site
  50. Sitepimp
  51. Social
  52. Spelling
  53. Syndication
  54. Testing
  55. The Future
  56. Thoughts
  57. Tools
  58. Tutorial
  59. Tutorials
  60. Typography
  61. UI
  62. UNIX
  63. Virtualisation
  64. Web
  65. Web Standards
  66. Widgets
  67. Wii
  68. Writing
  69. Xbox
  70. XHTML