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:
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:
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:
In the PHP5 example you’ll notice we don’t use the
$this keyword. This is simply because
$this could evaluate to either
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:
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
We can now access the
$sample_attribute of each brick within our
wall class like so:
Or from outside an instantiated object like so:
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:
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:
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:
In this example
$b point to the same content.
The same syntax can be used with functions, that return references, and with the
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:
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.