PHP Inheritance


PHP - What is Inheritance?

Inheritance in OOP = Inheritance occurs when one class inherits from another class.

The public and protected properties and methods of the parent class will be passed down to the child class. It may also have its own set of properties and methods.

The extends keyword is used to specify an inherited class.

Example :- A simple inheritence

<?php
class aParent {
  public $a = 10;

  public function printA() {
    echo "<br />A is " . $this->a;
  }
}

// aChild is inherited from aParent

class aChild extends aParent {
  public $b = 20;

  public function printB() {
    echo "<br />B is " . $this->b;
  }
}

$cc = new aChild();
$cc->printA();
$cc->printB();
?>

Output :-

A is 10
B is 20

Example Explained :-

From the aChild class, aParent class is inherited.

Due to inheritance, the aChild class can access the aParent class's public $a attribute, and printA() method.

The aChild class also includes a method called printB() that can be invoked.

Why We Need Inheritence

The main reason is use inheritence to make data and source code more reusability.

Example 1 :- We have created two normal classes "Add" and "Sub" without inheritence.

<?php
class Add
{
    public $a = 10, $b = 5;

    public function doAdd()
    {
        $c = $this->a + $this->b;
        echo "<br />Addition = " . $c;
    }
}

class Sub
{
    public $a, $b;

    public function doSub()
    {
        $c = $this->a - $this->b;
        echo "<br />Subtraction = " . $c;
    }
}
?>

In the above example, there is no any relations between both classes.

Now we want to make a relation between these classes. Ho to make it?

First, find common properties and methods from both classes and keep them in a seperate class (It will be parent class).

Next, keep remaining unique properties and methods in their classes ("Add" and "Sub") itself. Now these classes ("Add" and "Sub") are child classes.

And then, make inherit of parent class into child classes.

Example 2:- Making a parent and child class.

<?php
class AB
{
    public $a = 10, $b = 5;
}

class Add extends AB
{
    public function doAdd()
    {
        $c = $this->a + $this->b;
        echo "<br />Addition = " . $c;
    }
}

class Sub extends AB
{
    public function doSub()
    {
        $c = $this->a - $this->b;
        echo "<br />Subtraction = " . $c;
    }
}
?>

Note:- In the above programs, the $a and $b variables are created only once. So it makes code reusability.

You can also search for these topics, PHP OOP - Inheritance, What is php oop Inheritance, oop php type of inheritance, multiple inheritance oop php, php oop inheritance extend keyword, how to inherit the php oop class, Example for php oop inheritance.

PHP - Inheritance and the Protected Access Modifier

Protected properties or methods can be accessible both within and by classes inherited from the class.

Example 1 :- How to access a protected modifier from parent class.

<?php
class aParent {
  public $name = "abc";
  protected $age = 12;
}

// aChild is inherited from aParent

class aChild extends aParent {
  public $salary = 20000;

  public function set_Name_Age_Salary($name, $age, $salary)
  {
	$this->name = $name;	//	OK, Its public
	$this->age = $age;	//	OK, $age can be accessed in child class
	$this->salary = $salary;
  }

  public function printInfo() {
    echo "<br />Name = " . $this->name;
    echo "<br />Age = " . $this->age;
    echo "<br />Salary = " . $this->salary;
  }
}

$cc = new aChild();
$cc->printInfo();

/*
$cc->name = "xy";	//	OK, Its public
$cc->age = 23;	//	ERROR, because it is protected
$cc->salary = 15000;	//	OK, Its public
*/

$cc->set_Name_Age_Salary("xyz", 34, 18000);	//	OK, Accessed by a public member from child class
$cc->printInfo();
?>

Output :-

Name = abc
Age = 12
Salary = 20000
Name = xyz
Age = 34
Salary = 18000

You can also search for these topcis, PHP oop Inheritance and the Protected Access Modifier, properties or methods using PHP Inheritance and the Protected Access Modifier, php inheritance to derive a class protected access modifier, type of protected access modifier in php oop, Example for Inheritance and the Protected Access Modifier in php oop.

PHP - Overriding Inherited Methods

Redefining the methods in the child class (with the same name) can be used to override inherited methods.

Example :- Consider the following example. The aChild class (print()) methods will override the aParent class print() method.

<?php
class aParent {
  public $name = "abc";
  public function print() {
    echo "<br />Parent Name = " . $this->name;
  }
}

// aChild is inherited from aParent

class aChild extends aParent{
  public $name = "xy";
  public function print() {
    echo "<br />Child Name = " . $this->name;
  }
}

$cc = new aChild();
$cc->print();

?>

Output :-

Child Name = xy

You can also search for these topics, php inheritance override method, php override inhertance function, find the override inherited method in php oop, how to execute the parent class and child class in php Overriding Inherited Methods, Example for PHP Overriding Inherited Methods.

PHP - The final Keyword

Using the final keyword, you may prevent class inheritance or method overriding from happening.

Example 1 :- Class inheritance can be prevented using the following example :

<?php
final class Fruit {
}

class Strawberry extends Fruit {    //  ERROR
}

?>

Output :-

PHP Fatal error: Class Strawberry may not inherit from final class (Fruit) in /home/o6MTL0/prog.php on line 10

Example 2 :- The following example shows how to prevent method overriding :

<?php
class Fruit {
  final public function intro() {
  }
}

class Strawberry extends Fruit {
  // will result in error
  public function intro() {
  }
}
?>

Output :-

PHP Fatal error: Cannot override final method Fruit::intro() in /home/sdpysj/prog.php on line 15

You can also search for these topics, use php final keyword, php final keyword overriding method, how to prevent class inheritance with php final keyword, php property final keyword, Example for php final keyword.