Inheritance is a well-known programming notion, and PHP incorporates it into its object model. This concept will have an impact on how numerous classes and objects interact. Inheritance is a notion that enables us to specify properties of inheriting Classes without having to implement them. Code can be reused using the inheritance concept, which saves time. This tutorial talks about the notion of inheritance, where we will see Inheritance and the Protected Access Modifier, Overriding Inherited Methods, and prevent class inheritance or method overriding by using the keyword final.



What Is Inheritance in PHP?

The ability of a class to derive or inherit the properties and characteristics from another class is called inheritance. One of the most significant concepts in object-oriented programming is the idea of inheritance. Using the term extends, we inherit the characteristic of a parent class.

Syntax:

class ChildClass extends ParentClass

In the above syntax, ChildClass is a derived class (also called extended class and subclass) that extends ParentClass (also called superclass and base class).

Example:

<?php
/* Parent Class (also called superclass and base class)*/
class ParentClass
{
  function a()
  {
    echo "A member function of the base class.";
  }
}

/* Child class (also called Derived class, extended class and subclass)*/
class ChildClass extends ParentClass
{
  function b()
  {
    echo "A member function of the child class.";
  }
}

$Obj = new ChildClass(); /* Create object (child class instance) */
$Obj->a(); /*call member function of ParentClass*/
$Obj->b(); /*call member function of ChildClass*/
?>

Output:

A member function of the base class.
A member function of the child class.

Example:

<?php
/* Parent Class (also called superclass and base class)*/
class Phone {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  function intro() {
    echo "The phone is {$this->name} and, the color is {$this->color}.";
  }
}

/* Child class (also called Derived class, extended class and subclass)*/
class Apple extends Phone {
  function message() {
    echo "I am iPhone.";
  }
}

$Obj = new Apple("Apple iPhone 13 Pro", "pink");
$Obj->message();
$Obj->intro();
?>

Output:

I am iPhone.The phone is Apple iPhone 13 Pro and, the color is pink.

PHP Protected Access Modifier

Example:

<?php
/* Parent Class (also called superclass and base class)*/
class Phone {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  protected function intro() {
    echo "The phone is {$this->name} and, the color is {$this->color}.";
  }
}

/* Child class (also called Derived class, extended class and subclass)*/
class Apple extends Phone {
  function message() {
    echo "I am iPhone.";
  }
}

$Obj = new Apple("Apple iPhone 13 Pro", "pink");
$Obj->message();
$Obj->intro();
?>

Output:

I am iPhone.
( ! ) Fatal error: Uncaught Error: Call to protected method Phone::intro() from context '' in D:\wamp64\www\test1.php on line 26

Example:

<?php
/* Parent Class (also called superclass and base class)*/
class Phone {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  protected function intro() {
    echo "The phone is {$this->name} and, the color is {$this->color}.";
  }
}

/* Child class (also called Derived class, extended class and subclass)*/
class Apple extends Phone {
  function message() {
    echo "I am iPhone.";
    $this->intro();
  }
}

$Obj = new Apple("Apple iPhone 13 Pro", "pink");
$Obj->message();
?>

Output:

I am iPhone.
The phone is Apple iPhone 13 Pro and, the color is pink.

PHP Overriding Inherited Methods

Example:

<?php
/* Parent Class (also called superclass and base class)*/
class Phone {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  function intro() {
    echo "The phone is {$this->name} and, the color is {$this->color}.";
  }
}

/* Child class (also called Derived class, extended class and subclass)*/
class Apple extends Phone {
  public $weight;

  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }

  function message() {
    echo "I am iPhone.";
  }

  function intro() {
    echo "The phone is {$this->name} and, the color is {$this->color}.";
  }

}

$Obj = new Apple("Apple iPhone 13 Pro", "pink",210);
$Obj->message();
$Obj->intro();
?>

Output:

I am iPhone.The phone is Apple iPhone 13 Pro and, the color is pink.

PHP final Keyword

We can prevent class inheritance or method overriding by using the keyword final.

Example:

<?php
/* Parent Class */
final class Phone {
  function intro() {
  }
}

/* Child class */
class Apple extends Phone {
  function intro() {
  }
}
?>

Output:

( ! ) Fatal error: Class Apple may not inherit from final class (Phone) in D:\wamp64\www\test1.php on line 12

Example:

<?php
/* Parent Class */
class Phone {
  final function intro() {
  }
}

/* Child class */
class Apple extends Phone {
  function intro() {
  }
}

$Obj = new Apple();
$Obj->intro();
?>

Output:

( ! ) Fatal error: Cannot override final method Phone::intro() in D:\wamp64\www\test1.php on line 10


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram