Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. Multiple inheritance in PHP PHP does not support Multiple Inheritance but using interfaces in PHP, we can implement it. Using class with Traits: The trait is a type of class that enables multiple inheritance. Classes, objects, and traits that do not extend more than one class, but it can extend multiple traits at the same time. Syntax:

class child_class_name extends parent_class_name
{
    use trait_name;
   ------
   ------
    child_class functions
}
Example:
<?php
// Class Inheritance
class Inheritance
{
public function example()
{
            echo "Hi";
}
}
// Trait formultiple
trait formultiple
{
public function examplem()
{
            echo "\t Traits";
}
}
class Sample extends Inheritance
{
use formultiple;
public function MultipleInheritance()
{
            echo "\n Multiple-Inheritance";
}
}
$check = new Sample();
$check->example();
$check->examplem();
$check->MultipleInheritance();
?>
Output:
Hi Traits Multiple-Inheritance
  • In the above example, “traits” is used with parent class. There is “class” named “Inheritance” which contains function example() and “trait” named “formultiple” that contains function “examplem()” and a child class named “Sample” and we are creating the object of this class named “check” and we are invoking all the functions of a class and a trait.

Using Multiple Traits:

Multiple Traits can be added into a class by listing them in the use statement, which is separated by commas. Syntax:
class child_class_name 
{
use trait_name;  
----    
----    
child_class functions
}
Example:
<?php
// trait Multiple
trait Multiple
{
public function example()
{
            echo "Hii";
}
}  
trait formultiple
 {
public function exam()
{
            echo " Multiple";
}
}
class Test
{
use Multiple;
use formultiple;
public function MultipleInheritance()
{
            echo "\n MultipleInheritance";
}
}
$check = new Test();
$check->example();
$check->exam();
$check->MultipleInheritance();
?>
Output:
Hii Multiple MultipleInheritance
  • In the above example, the “traits” have been used. There are two traits named “Multiple” that contain function example() and “multiple” that include function exam(), and there is a child class “Test” and we are creating the object of that class named “check” and we are invoking all the functions of traits.

Using Class along with Interface

Syntax:
class childclass extends parentclass implements interface1, …
Example:
<?php
class A
{
public function insideA()
{
            echo "A";
            }
}
interface B
 {
public function insideB();
}
class Multiple extends A implements B
 function insideB()
{
                        echo "\n Interface";
            }
public function insidemultiple()
{
            echo "\n Inherited";
            }
}
$check = new multiple();
$check->insideA();
$check->insideB();
$check->insidemultiple();
?>
Output:
A Interface Inherited
  • In the above example, Interface “B” used with the class “A” to implement multiple inheritances. The important point is that it cannot define the function inside the interface; it should be set inside the child class “Multiple”. We are calling all the functions using the child (Multiple) objects named “check”.

Multiple Interface

Example:
<?php
interface C
{
public function interfaceC();
}
interface B
 {
public function interfaceB();
}
class Multiple implements B, C
{
// Interface B
            function interfaceB()
 {
                        echo "\n InterfaceB";
            }

// Interface C
            function interfaceC()
 {
                        echo "\n InterfaceC";
            }

            public function insidemultiple()
            {
                        echo "\n Inherited class";
            }
}
$test = new multiple();
$test->interfaceC();
$test->interfaceB();
$test->insidemultiple();
?>
Output:
InterfaceC InterfaceB Inherited class
In the above example, multiple interfaces have been used to implement multiple inheritances. There are two interfaces called “B” and “C” that are base classes with the child class that is called “Multiple” and we are calling the functions using its object "test".