Multiple Inheritance in PHP
Last Updated :
01 Aug, 2021
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:
<?php
class Geeks {
public function sayhello() {
echo "Hello" ;
}
}
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();
?>
|
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:
<?php
trait Geeks {
public function sayhello() {
echo "Hello" ;
}
}
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();
?>
|
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:
<?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();
?>
|
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:
<?php
interface C {
public function insideC();
}
interface B {
public function insideB();
}
class Multiple implements B, C {
function insideB() {
echo "\nI am in interface B" ;
}
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();
?>
|
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”.
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.