PHP Inheritance

Summary: in this tutorial, you will learn about the PHP inheritance that allows a class to reuse the code from another class.

Introduction to the PHP inheritance

Inheritance allows a class to reuse the code from another class without duplicating it.

In inheritance, you have a parent class with properties and methods, and a child class can use the code from the parent class.

Inheritance allows you to write the code in the parent class and use it in both parent and child classes.

The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass.

To define a class inherits from another class, you use the extends keyword.

Suppose that you have a class called BankAccount as follows:

<?php

class BankAccount
{
    private $balance;

    public function getBalance()
    {
        return $this->balance;
    }

    public function deposit($amount)
    {
        if ($amount > 0) {
            $this->balance += $amount;
        }

        return $this;
    }
}Code language: HTML, XML (xml)

The BankAccount class has the private property $balance and the public methods getBalance() and deposit().

To declare that the SavingAccount inherits from the BankAccount, you use the extends keyword as follows:

<?php

class SavingAccount extends BankAccount
{
}Code language: HTML, XML (xml)

In this example, the SavingAccount can reuse all the non-private properties and methods from the BankAccount class.

The following creates a new instance of the SavingAccount class, calls the deposit() method and shows the balance:

<?php

require 'SavingAccount.php';

$account = new SavingAccount();
$account->deposit(100);
echo $account->getBalance();
Code language: HTML, XML (xml)

In this example, the SavingAccount class reuses the properties and methods from the BankAccount class.

A child class can reuse properties and methods from the parent class. But the parent class cannot use properties and methods from the child class.

Add properties and methods to the child class

A child class can have its own properties and methods. In the following example, we add the $interestRate property and setInterestRate() method to the SavingAccount class:

<?php

class SavingAccount extends BankAccount
{
    private $interestRate;

    public function setInterestRate($interestRate)
    {
        $this->interestRate = $interestRate;
    }
}Code language: HTML, XML (xml)

Call methods from the parent class

To call a method from the parent class, you use the $this keyword.

The following example defines the addInterest() method that calculates the interest and adds it to the balance:

<?php

class SavingAccount extends BankAccount
{
    private $interestRate;

    public function setInterestRate($interestRate)
    {
        $this->interestRate = $interestRate;
    }

    public function addInterest()
    {
        // calculate interest
        $interest = $this->interestRate * $this->getBalance();
        // deposit interest to the balance
        $this->deposit($interest);
    }
}Code language: HTML, XML (xml)

In the addInterest() method, we call the getBalance() method from the BankAccount class for calculating the interest and the deposit() method to add the interest to the balance.

Put it all together:

<?php

class BankAccount
{
    private $balance;

    public function getBalance()
    {
        return $this->balance;
    }

    public function deposit($amount)
    {
        if ($amount > 0) {
            $this->balance += $amount;
        }

        return $this;
    }
}

class SavingAccount extends BankAccount
{
    private $interestRate;

    public function setInterestRate($interestRate)
    {
        $this->interestRate = $interestRate;
    }

    public function addInterest()
    {
        // calculate interest
        $interest = $this->interestRate * $this->getBalance();
        // deposit interest to the balance
        $this->deposit($interest);
    }
}

$account = new SavingAccount();
$account->deposit(100);
// set interest rate
$account->setInterestRate(0.05);
$account->addInterest();
echo $account->getBalance();
Code language: HTML, XML (xml)

Inhertiance and UML

You use the open arrow from the child class to the parent class to represent the inheritance relationship. The following UML diagram shows the inheritance relationship between the BankAccount and SavingAccount classes:

Summary

  • Inheritance allows a class to reuse the code of another class without duplicating it.
  • Use the extends keyword to define one class that inherits from another class.
  • A class that inherits another class is called a subclass, a child class, or a derived class. The class from which the subclass inherits is a parent class, a superclass, or a base class.
  • A subclass can have its own properties and methods.
  • Use $this keyword to call the methods of the parent class from methods in the child class.
Did you find this tutorial useful?