Multiple Inheritance in PHP

Multiple Inheritance is the property of the Object Oriented Programming languages in which child class or sub class can inherit the properties of the multiple parent classes or super classes.

PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it.

Traits (Using Class along with Traits): The trait is a type of class which enables multiple inheritance. Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.
Syntax:

class child_class_name extends parent_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Class Geeks
class Geeks {
  public function sayhello() {
     echo "Hello";
  }
}
  
// Trait forGeeks
trait forGeeks {
  public function sayfor() {
     echo " Geeks";
  }
}
  
class Sample extends Geeks {
   use forGeeks;
   public function geeksforgeeks() {
      echo "\nGeeksforGeeks";
  
}
  
$test = new Sample();
$test->sayhello();
$test->sayfor();
$test->geeksforgeeks();
?>

chevron_right


Output:

Hello Geeks
GeeksforGeeks

In the above program “traits” has been used along with parent class. There is “class” named “Geeks” which contains function sayhello() and a “trait” named “forGeeks” which contains function geeksforgeeks() and there is child class named “Sample” and we are creating the object of this class named “test” and using it we are invoking all the functions of a class and a trait.

Traits (Using Multiple Traits): Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas.
Syntax:

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// trait Geeks
trait Geeks {
  public function sayhello() {
     echo "Hello";
   }
 }
  
// trait forGeeks
trait forGeeks {
  public function sayfor() {
     echo " Geeks";
   }
 }
  
class Sample {
  use Geeks;
  use forGeeks;
  public function geeksforgeeks() {
      echo "\nGeeksforGeeks";
   
}
  
$test = new Sample();
$test->sayhello();
$test->sayfor();
$test->geeksforgeeks();
?>

chevron_right


Output:

Hello Geeks
GeeksforGeeks

In the above program “traits” has been used. There are two traits named “Geeks” which contains function sayhello() and “forGeeks” which contains function geeksforgeeks() respectively and there is a child class “Sample” and we are creating the object of this class named “test” and using it we are invoking all the functions of traits.

Interface (Using Class along with Interface):
Syntax:

class child_class_name extends parent_class_name implements interface_name1, ...

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
class A {
   public function insideA() {
    echo "I am in class A";
     }
}
  
interface B {
   public function insideB();
}
  
class Multiple extends A implements B {
  
    function insideB() {
        echo "\nI am in interface";
    }
  
    public function insidemultiple() {
    echo "\nI am in inherited class";
    }
}
  
$geeks = new multiple();
$geeks->insideA();
$geeks->insideB();
$geeks->insidemultiple();
?>

chevron_right


Output:

I am in class A
I am in interface
I am in inherited class

In the above program Interface “B” has been used along with the class “A” to implement multiple inheritance. The important point to remember is, it can’t define the function inside interface, it should be defined inside the child class “Multiple”. We are invoking all the functions using the child class (Multiple) object named “geeks”.

Interface (Using Multiple Interface):

Syntax:

class child_class_name implements interface_name1, interface_name2, ...

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
interface C {
   public function insideC();
}
  
interface B {
   public function insideB();
}
  
class Multiple implements B, C {
  
    // Function of the interface B
    function insideB() {
        echo "\nI am in interface B";
    }
  
    // Function of the interface C
    function insideC() {
        echo "\nI am in interface C";
    }
  
    public function insidemultiple()
    {
        echo "\nI am in inherited class";
    }
}
  
$geeks = new multiple();
$geeks->insideC();
$geeks->insideB();
$geeks->insidemultiple();
?>

chevron_right


Output:

I am in interface C
I am in interface B
I am in inherited class

In the above program multiple interfaces has been used to implement multiple inheritance. In above example there are two interfaces named “B” and “C” those are playing the role of the base classes and there is child class named “Multiple” and we are invoking all the functions using its object named “geeks”.



My Personal Notes arrow_drop_up

I am a quick learner and eager to learn new technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.