Inheritance in PHP


In this tutorial, we will learn about Inheritance in PHP. Inheritance is a mechanism in which we can inherit the properties and methods of one class into another. This helps us to create a new class that will have its own properties and methods and all the features of its parent class(from which the new class is inheriting features). While performing inheritance you will see two kinds of classes as follow:

  • Parent Class: The which being inherit.
  • Child Class: The class which is inheriting features of another class:

Note, extends keyword is used to inherit faetures of on class into another

Available Types of Inheritance in PHP:

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

 I know, those students who have background in c++ might be missing Multiple Inheritance. But Multiple Inheritance is not available in PHP.

Single Level Inheritance:

In single-level inheritance, we inherit one class into another class. In this process, One is parent class and the other is child class. This type of inheritance also known as simple inheritance.

Single Level Inheritance in PHP

Example Program:

<?php

//Parent Class
class Person {
    
   protected $name;
   
   function set_name($name){
      $this->name = $name;
    }
}

//child class inheriting Person class
class Student extends Person {

  function say_hello(){
     echo "Hello $this->name, How is your study going on?";
  }
}

$student1 = new Student;
$student1->set_name("Rahul");
$student1->say_hello();

?>

As in the above example program, you can see $name variable and set_name() method are not written in the Student class but we are accessing them with the object of Student class. This is happing because after inheriting the Person class in the Student class the properties and methods become part of the Student class.

Multilevel Inheritance:

In this type of inheritance, there will be more than 2 classes involved. In Multilevel inheritance Parent class will be inherited by a child and then further child class will be inherited by another child class.Multilevel Inheritance in PHP

In above scanrio, B is parent of C class and also A is indrect parent of C class.

Example Program:

<?php

class A{
  function showA(){
    echo "I am show method in A Class<br>";
  }
}

class B extends A{
  function showB(){
    echo "I am show method in B Class<br>";
  }
}
  
class C extends B{
  function showC(){
    echo "I am show method in C Class<br>";
  }
}
  
 
$obj=new C;
$obj->showA();
$obj->showB();
$obj->showC();

?>

Hierarchical Inheritance:

When more than one class inherit to a single class. This type of inheritance known as Hierarchical Inheritance. Please check the following illustration to understand this:Hierarchical Inheritance in PHP

Example Program:

<?php

class Greeting{
   function welcome($str){
     echo "Hello, We welcome you $str <br>";
   }
}
 
class Teacher extends Greeting{
      
   
}
 
class Student extends Greeting{
 
   
}

$teacher = new Teacher;
$teacher->welcome("Sir");

$student =new Student;
$student->welcome("dear");

?>

 



Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share