Ten minutes To PHP Objects
Written by Mike James   
Thursday, 18 November 2021
Article Index
Ten minutes To PHP Objects


Put simply the basic idea of inheritance is to use an existing class to build a new class that is a customisation or extension of it. You can think of this as just a way to save typing or as a deep principle of program construction. In PHP programs inheritance tends to be less useful to the typical application programmer. It is of more use if you plan to build a framework for others to use.

For example, let’s suppose that the Calculator class is completely finished and working and we need a special type of calculator – perhaps a scientific calculator.

We could take all of the PHP code in the Calculator class and use it to create a new SciCalc class using nothing but copy and paste. We could then add methods and even modify existing methods “inherited” via copy and paste from Calculator.

This all works but it produces a problem for the future. Now we have two sets of more or less identical code – Calculator and the code we copied and pasted into SciCalc. Now imagine what happens when an error is found in Calculator. You will have to make any changes that fix the error in Calculator in SciCalc and vice versa. In short you now have two copies of more or less the same code to debug and maintain – not good.

Inheritance as implemented in most object-oriented languages is just an attempt to improve on copy and paste by keeping the relationship between the classes alive.

For example, in PHP you could create the SciCal class, which inherits everything Calculator has, using:

class SciCal extends Calculator

Following this, and even without writing a single line of code, SciCal has all of the methods and properties of Calculator.

That is, if you create an instance of SciCal the object can be used exactly if it was an instance of Calculator. This is just like the copy and paste replication only now if you make any changes to Calculator these are passed on immediately to SciCal without you having to do anything about it.

What do you do, however, if you want to change the way an inherited method works?

The answer is that you can override it by simply providing a new definition. For example, if the SciCal class needed to implement an addition method with worked to a higher precision say then it would simply include a new definition of the method. Any inherited methods that are redefined in this way are replaced or ‘overridden’ by their new definition.

There are a couple of minor points we need to deal with. The first is that when you override a method it isn’t destroyed – it still exists but the new method is used in place of it. PHP, and most object-oriented languages, provide a mechanism whereby you can make use of the original version of the method if you really want to.

All you have to write is


and note that you use a double colon ::  in place of ->.

For example, if you have overridden add in the SciCal class which inherits from Calculator then, within the new add function, you could write:


to call the original add method. Notice that you can only use this syntax within the SciCal class.

Another subtle point is what happens to constructors when a class is inherited?

The answer depends on whether or not you provide a constructor for the new class. If you don’t provide a constructor and the base class has one then it is called automatically. That is, if the Calculator class has a constructor which takes one parameter then so does SciCalc and you have to create an instance of the class using:

$mycalc=new SciCalc(1);

even if you haven’t defined a constructor for SciCalc.

If you do define a constructor for the new class then it is used and the base class’s constructor isn’t automatically called. You can use


in the new class to explicitly call it – but you don’t have to if you don't need the initialization if provides.

In all of this discussion you need to keep in mind that you can use inheritance more than once. That is, class A can be derived from class B, which was derived from class C and so on. As said earlier however most uses of PHP don't really depend on inheritance and long inheritance chains are unusual.


A button pad HTML object

It is difficult to give a really good example of how to make use of the object-oriented features of PHP – or indeed any object-oriented language. The reason is that the real advantages of the object-oriented approach really only become clear when the project is large and complicated - and large and complicated examples generally aren’t clear!

To give you some idea of how you might go about using the object-oriented approach in PHP let’s build a simple class which generates a particular HTML object.

A common requirement is a set of buttons arranged in a grid – hence our class is called buttonpad. Given that our earlier example involved a calculator you can see that a buttonpad object might well form the basis of a calculator object’s user interface.

The start of the class simply defines some variables that are used:

class buttonpad
 public $hor;
 public  $ver;

The $hor and $ver properties are going to be used to store the dimensions of the button grid. You can save the code for the class in a file called “buttonpad.inc” for use in a PHP page.

Getting just a little more advanced we can also make use of an array to store the labels that are going to be displayed on each of the buttons. This array can be accessed to set the labels or methods can be provided to set it automatically:

public $names=array();

The constructor simply sets the size of the buttonpad n x m buttons:

function __construct($n,$m)

As an example of a method to set the button labels automatically, the following sets them to 1,2,3,4 and so on reading right-to-left and top-to-bottom:

public function SetSequentialLabels()

This complicated-looking function simply stores consecutive values in the array names.

Last Updated ( Thursday, 18 November 2021 )