PHP Inheritance

Summary: in this tutorial, you will learn how to use PHP inheritance technique that helps reuse the logic of existing classes. You will also learn about the the overriding methods as well as the final classes and methods concepts.

PHP inheritance introduction

The idea of inheritance powerful. It allows you to create a new class that reuses the properties and methods from an existing class. A class that inherits from another class is called subclass (also a child class or a derived class). The class from which the subclass inherits is known as parent class (also a superclass or a based class). Besides inheriting properties and methods from the parent class, a subclass class can have additional properties and methods.

Inheritance is very useful if you want to create several similar classes. You put the common properties and methods in the parent class and specific properties and methods in the child classes. This helps you avoid duplicate code that implements the same properties and method in many classes.

PHP inheritance example

In the PHP objects and classes tutorial, we created a bank account ( BankAccount) class. We can extend the bank account class by creating other bank account classes e.g., saving account class and checking account class that reuse the properties and methods of the BankAccount class:

  • Saving account : is a regular bank account and earns monthly interest.
  • Checking account: is a regular bank account, earns no interest and has fee deducted monthly.

Inheritance UML diagram

We can model the relationships between BankAccount, SavingAccount and CheckingAccount classes using UML class diagram as follows:

PHP Inheritance Class Diagram

PHP Inheritance – Class Diagram

Class files structure

We typically put the code of each class in a separate PHP file that has the same name as the class name. All class files usually locates in a folder named classes as following:

PHP inheritance class files

Source code example

To inherit from a class, you use the extends keyword. The following example illustrates how the SavingAccount class inherits from the BankAccount class:

Let’s examine the SavingAccount class in greater detail.

First, to inherit from the BankAccount class, the extends keyword is used as follows:

Second, inside the constructor of the SavingAccount class, we called the constructor of the BankAccount class using the following syntax:

In addition, we also initialize the interest rate in the constructor of the SavingAccount class.

Third, we add a the addInterest() method to the SavingAccount class. In this method, we called the methods of the parent class ( BankAccount) using the following syntax:

and

As you see, the logic of the addInterest() method of the SavingAccount class reuses some logic of the methods in the parent class ( BankAccount).

Let’s test the new SavingAccount class.

We can do it the same way for the CheckingAccount class.

Overriding methods

To make the method of a subclass behave differently, you need to override a method in the parent class by creating the same method name in the subclass. Inside the new method in the subclass, you can use logic from the method of the parent class or have its own logic.

Let’s take a look at an example of overriding a method.

In the BankAccount class, we can add the __toString() magic function so that we can display a bank account object as a string using echo() function. The following is __toString() function implementation:

In the SavingAccount subclass, we can use the same __toString() function. The __toString() function in the SavingAccount class overrides the __toString() function in the BankAccount class.

Notice that we preserve the logic of the __toString() function in the BankAccount class by calling its __toString() function in the subclass i.e. SavingAccount:

Now if we call the __toString() function of the SavingAccount object, its logic will take effect.

Final classes and methods

Final class

A final class cannot be inherited from any other classes. To make a class that it cannot be inherited from, you use the final keyword as follows:

If there is a class that inherits from a final class, PHP will issue a fatal error. Why would you need to prevent other classes inheriting from a class? There a two main reasons:

  • Security: one of the common technique that malicious hackers use to exploit the system is to create a subclass of a class and substitute the parent class by the subclass. The subclass is similar to the parent class but it behaves differently that cause the damage to the system.
  • Design: another reason to make your class final is your object-oriented design. You may think that your class is “perfect” and it should not have any subclass.

Final method

The final method of a class does not allow method the subclasses to override it. By doing so, you ensure that the method in your class will behave consistently.

You use the final keyword to make a method final as follows:

You can download the script files for the PHP inheritance tutorial via the following link:

PHP Inheritance (1395 downloads)

In this tutorial, we have shown you how to use PHP inheritance to create new classes that not only inherit functionality from an existing class but also have their own specific properties and methods. In addition, we introduced the overriding methods as well as the final classes and methods concepts so that you can apply them to make your object-oriented design more flexible.

  • Was this tutorial helpful ?
  • YesNo