Having spent the last few months writing this tutorial, I’ve realised that it’s far too long for a single post; so I’m splitting it into several to make it easier to digest. Here’s a quick summary of what I’ll be covering in each segment:
- Definition - A look at defining classes and objects.
- Relationships - Making classes and objects work together.
- Taking Relationships Further - Visibility and polymorphism.
- PHP5's Improved Features - More object-oriented functionality.
So, without further ado, let’s dive straight into Part 1: Definition.
Part 1: Definition
Adopting object-oriented techniques when developing your PHP scripts and applications can immediately inject greater flexibility and easier maintenance into your development environment. Programs become easier to extend, or debug, and sharing code with other developers on your team (should you have one) becomes simpler.
Objects and Classes
There's No Substitute for Class
To define a class in PHP, we write the following code:
To instantiate our class (ie. create an instance), we need to use the
new keyword - much like the
This creates an object. An object is, in effect, a new variable with a user-defined data-type (the class). This basically means we could instantiate our class over and over into as many different objects as we so desire.
Any New Members?
To access the various attributes and methods (members) of our object, we can use the namespace separator
-> (sometimes called the arrow operator; although it’s not really an operator). When you are accessing a variable in PHP, the part of code after the
$ is known as the namespace. When you are accessing attributes and methods that are members of an object, you need to extend that namespace to include the name of your object. To do this, you reference attributes and methods like so:
This idea of namespace and namespace separators is very similar to a standard directory structure - the slashes or backslashes becoming the
-> separator. Since we can have objects within objects, we can also extend this further. For example, if our
$myObject object was a member of another object called
$anotherObject, we could reference the members of
$myObject like so:
Notice that the
$ appears only once before the namespace; this is because a variable name in PHP can only ever have a single dollar sign. If we were to place another
$ anywhere in our namespace, we’d be telling PHP we were using variable variables. I don’t really want to go too far off topic here, so have a look at the PHP documentation for more information about variable variables.
When writing code within a class definition, you’re never going to know the name of the object to which it belongs; this is because the object hasn’t even been instantiated at this point. In order to access the attributes and methods of your class from within the class itself, we will need to use the
$this can also be thought of as ‘my own’ or ‘current object’. In our example, we’re returning the value of
$attribute1 within the ‘current object’.
The constructor of a class is a special method that is run upon instantiation (when an object is created). Itâ€™s main purpose is to set-up the attributes of a class and to establish the class invariant - to basically make sure that the attributes of the class conform to the class interface (a class interface is inferred in PHP4 but can be defined in PHP5; we’ll talk about that in part 5 of this tutorial since, for now at least, it’s unimportant). A properly written constructor should never contain any functionality that could fail, thus leaving the object in an invalid state.
In PHP4, we define our constructor by giving the function the same name as the class itself. Here’s an example:
In this example the constructor receives four parameters. The values of these parameters are then passed to the four class attributes. This is usually the kind of action that is taken by the constructor - it avoids us having to directly set the value of our class attributes which, although possible, would undermine our class interface.
We can now instantiate the class like so - specifying our object’s attributes as we go:
In PHP5, however, we declare our constructor using the new
It’s worth noting that the method used to declare a constructor in PHP4 (ie. naming it the same as its class) will still function as expected in PHP5. This is to allow for backwards compatibility.
A destructor function is run when an object is destroyed - its role is to clean up and to free the resources which were used by the object during run-time, and to unlink the object from other objects or resources.
In PHP4 this functionality is sadly unavailable, although it’s perfectly possible to write a specific destructor method within your class and run it before you destroy the object. You can take this idea a step further by registering your method to run when script processing is complete using the
register_shutdown_function function like so:
For more information on this, you should read the
register_shutdown_function entry in the PHP documentation.
PHP5 allows us to specify an object destructor using the
__destruct function. Here’s an extention of our constructor example that assumes we might have created a MySQL connection:
Because PHP is a scripting language and is therefore stateless, the role of the destructor is often negated by the ending of script execution. However the destructor is still useful should this not be the case. It can be argued, however, that you should always include a destructor simply so that you can always be assured of clean object destruction.
You should be fairly confident in creating classes and objects now; allowing increased modularity and encapsulation within your PHP scripts. As I stated earlier in my “Object-Oriented Concepts” post, OOP provides greater flexibility and easier maintainance across large systems and can sometimes make understanding and analysing complex procedures a lot easier.
In the next post, I’ll be taking this functionality to the next level by introducing object-oriented relationships into our PHP.