Introduction to Multiple Inheritance in PHP
Multiple Inheritance is one of the best property of the Object-Oriented Programming Languages like PHP in which subclass or class can inherit/inherits the properties of the multiple superclasses or multiple parent classes. PHP programming language doesn’t even support the multiple inheritance/inheritances. PHP supports multiple inheritances only by using interfaces or Traits in PHP instead of classes so that we can implement it.
Traits:
Traits are a type of class that enables multiple case classes, objects, classes, and traits. Traits only extend multiple traits at the same time but can’t extend more than one class.
Syntax:
class child_class extends parent_class{
use trait name;
..
..
Child_class function/functions
}
How Does Multiple Inheritance Work in PHP?
Multiple inheritances works by inheriting the properties of the multiple super classes or parent classes to a subclass or the child class etc. Multiple inheritances does not work directly but it can work by using the Traits concept.
Examples of Multiple Inheritance in PHP
Below are the examples of multiple inheritances in PHP:
Example #1
In the below example of the program “traits” are used along with the parent class. Here there is a class with the name “Pavan” which has a function sayhihello() and also a “trait” with the name “forPavan” which contains a function called pavanforNaruto() and there is also a child class with the name “Sample” and we are now here creating the object of the class with the name “test” and now using it to invoke all the functions of the trait and the class.
Code:
<?php
// Class Pavan
class Pavan {
public function sayhihello() {
echo "Hey Hi Hello";
}
}
// Trait for pavan
trait forPavan {
public function sayforp() {
echo " Pavan";
}
}
class Sample extends Pavan {
use forPavan;
public function pavanforNaruto() {
echo "\npavanforNaruto";
}
}
$test = new Sample();
$test->sayhihello();
$test->sayforp();
$test->pavanforNaruto();
?>
Output:
Example #2
Listing the traits by inserting them into the class in the use statement and it is also separated by the commas. In the below-stated program’s examples, “traits” are used. There are only two traits with the name “Pavan” which has a function called sayhihello() and “forPavan” which has the function pavanforNaruto() and there is also a child class called “Sample” now here we are creating class object with the name “test” and invokes all the traits functions.
Code:
<?php
// trait Pavan
trait Pavan {
public function sayhihello() {
echo "Hey Hi Hello";
}
}
// trait forpavan
trait forPavan {
public function sayforp() {
echo " Pavan";
}
}
class Sample {
use Pavan;
use forPavan;
public function pavanforNaruto() {
echo "\nPavanforNaruto";
}
}
$test = new Sample();
$test->sayhihello();
$test->sayforp();
$test->pavanforNaruto();
?>
Output:
Example #3
This is the Interface Program. The below example is done by using the class along with the interface. In the below program Interface “B1” has been used with the class “A1” in order to implement the multiple inheritances. The main important point in order to remember is that it can’t be defined by the function inside of the interface. It will be/should be defined inside of the child class “Multiple1”. No one can invoke all the other functions using the child class which is multiple with the object name “pavan”.
Code:
<?php
class A1 {
public function insideA1() {
echo "Hi buddy you are in class A1";
}
}
interface B1 {
public function insideB1();
}
class Multiple1 extends A1 implements B1 {
function insideB1() {
echo "\nHi buddy you are in the interface";
}
public function insidemultiplepavan() {
echo "\nHey now you are in the inherited class";
}
}
$Naruto = new multiple1();
$Naruto->insideA1();
$Naruto->insideB1();
$Naruto->insidemultiplepavan();
?>
Output:
Example #4
In the below example program there are multiple interfaces that are very much helpful in implementing the multiple inheritances. In the below-listed PHP program’s example, there are two interfaces with the names “B1” and “C1” which are actually playing the role of the base classes and there is always a child class with the name “Multiple1” and we are now invoking all the other functions by using the “pavan” object.
Code:
<?php
interface C1 {
public function insideC1();
}
interface B1 {
public function insideB1();
}
class Multiple1 implements B1, C1 {
// Function of the interface B1
function insideB1() {
echo "\n Hey now you are in the interface B1";
}
// Function of the interface C1
function insideC1() {
echo "\nHi buddy !! Now you are in the interface C1";
}
public function insidemultiple1()
{
echo "\nHeyaa You are in the inherited class";
}
}
$Naruto = new multiple1();
$Naruto->insideC1();
$Naruto->insideB1();
$Naruto->insidemultiple1();
?>
Output:
Example #5
In the below example of the PHP multi inheritance program, we are actually using the “trait” in order to enable the multi inheritance features. The “use” term is for using the “trait” functionality. The class CompName1 shows how to start using “trait”.
Code:
<?php
class BaseClass1
{
public function Hello1()
{
echo 'Hello MCN ';
}
}
trait OtherCls1
{
public function Hello1()
{
parent::Hello1();
echo ' Now i have the Solution!';
}
}
class CompName1 extends BaseClass1
{
use OtherCls1;
}
$obj = new CompName1();
$obj->Hello1();
?>
Output:
Example #6
This is a simple PHP program which is to illustrate how to use class with the traits. Trait is basically a type of class that helps to enable the multiple inheritance concept. Classes, traits, and objects which don’t extend more than a class/more than one class, but here it can extend to numerous traits at the same time. In this example, traits actually used with the parent class. It also has Inheritance1 class which has a function with the name example1(), formultiple1 trait which has a function called examplem1() and with a child class name “sample1” to also creating the object of the class “check1” and here invoking all the functions of the trait and the class.
Code:
<?php
// Class Inheritance
class Inheritance1
{
public function example1()
{
echo "Hey";
}
}
// Trait formultiple1
trait formultiple1
{
public function examplem1()
{
echo "\t My Traits";
}
}
class Sample1 extends Inheritance1
{
use formultiple1;
public function MultipleInheritance1()
{
echo "\n I am in the Multiple-Inheritance1";
}
}
$check = new Sample1();
$check->example1();
$check->examplem1();
$check->MultipleInheritance1();
?>
Output:
Recommended Article
This is a guide to the Multiple Inheritance in PHP. Here we discuss how Multiple Inheritances works in PHP and its examples along with Code Implementation. You can also go through our other suggested articles to learn more-