The following code shows an Example of Inheriting a Class in PHP.

As can be seen in the following, code, there is a Person class. Another class is Customer which is derived from the Person class. Furthermore, the Person class has three attributes – pname, age, and city. These attributes represent the name of person, his/her age, and city respectively. Since, these attributes are declared as protected in the base class, so these are also available in the derived class Customer.

Apart from these protected attributes, the Person class also has a constructor, and a destructor. Since in PHP, the default access modifier is public. Therefore, the functions declared without any access modifier is by default a public function.

After that, we need to create a subclass of the Person class. Therefore, we derive a class named Customer from the Person class. The following code shows the syntax of deriving a class.

Inheriting a Class in PHP

class <base-class-name>
{
   //attributes and methods
}
class <derived-class-name> extends <base-class-name>
{
   //attributes and methods 
}

Hence, in the following example, we use the extends keyword to inherit a class. Since, the base class has three protected attributes. So, the derived class Customer inherits them. Also, it has two more attributes of its own. Further, note that how to access a base class method or constructor in the subclass. For the purpose of calling a base class method in the subclass, we use the following syntax: parent::<base-class-method-name>.

As can be seen in the example, the constructor in the subclass calls the base class constructor first. Similarly, the destructor in the subclass also calls the base class destructor. Finally, we create an object of the subclass Customer. As a result, the corresponding constructors execute.

<?php
  class Person
  {
	protected $pname;
        protected $age;
        protected $city;

        function __construct($pname, $age, $city)
        {
		echo 'Inside Base Class Constructor...<br>';
		$this->pname=$pname;
		$this->age=$age;
		$this->city=$city;
	}
	function __destruct()
	{
		echo 'Inside Base Class Destructor...<br>';		
	}
  }
  class Customer extends Person
  {
	private $order_value;
        private $order_quantity;
        function __construct($pname, $age, $city, $value, $q)
        {
		parent::__construct($pname,$age, $city);
		echo 'Inside Derived Class Constructor...<br>';
		$this->order_value=$value;
		$this->order_quantity=$q;

	}
	function __destruct()
	{
		parent::__destruct();
		echo 'Inside Derived Class Destructor...<br>';		
	}
	function show_details()
	{
		echo '<br>Customer Details...<br>';
		echo 'Customer Name: ',$this->pname, '<br>';
		echo 'Age: ',$this->age, '<br>';
		echo 'City: ',$this->city, '<br>';
		echo 'Order Value: ',$this->order_value, '<br>';
		echo 'Order Quantity: ',$this->order_quantity, '<br>';
	}
  }

  $ob=new Customer('Pranav', 20, 'Delhi', 2000, 19);
  $ob->show_details();
?>
<br>
<br>

Output

Demonstrating an Example of Inheriting a Class in PHP
Demonstrating an Example of Inheriting a Class in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP