Is multiple inheritance supported in PHP?
Answer
PHP supports only single inheritance; meaning is a class can be extended from only one single class using the keyword 'extended'.
But still we can achieve using some mechanism Trait or Interface
xxxxxxxxxx
<?php
interface IWheel {
public function FunWheelType();
}
interface IEngine {
public function FunEngineType();
}
class Car implements IEngine, IWheel {
function FunEngineType() {
echo "I am in interface IEngine"."\n"; //interface IEngine function
}
function FunWheelType() {
echo "I am in interface IWheel"."\n"; //interface IWheel function
}
public function FunCar()
{
echo "I am in inherited class"."\n";
}
}
$obj = new Car();
$obj->FunEngineType();
$obj->FunWheelType();
$obj->FunCar();
?>
Is multiple inheritance supported in PHP?
Answer
PHP supports only single inheritance; meaning is a class can be extended from only one single class using the keyword 'extended'.
But still we can achieve using some mechanism Trait or Interface
xxxxxxxxxx
<?php
trait TGrandFather {
public function FunGrandFather() {
echo "I am from Trait TGrandFather"."\n";
}
}
trait TFather {
public function FunFather() {
echo "I am from Trait TFather"."\n";
}
}
class SonClass {
use TGrandFather;
use TFather;
public function FunSonClass() {
echo "I am from class SonClass"."\n";
}
}
$obj = new SonClass();
$obj->FunGrandFather();
$obj->FunFather();
$obj->FunSonClass();
?>
Is multiple inheritance supported in PHP?
Answer
PHP supports only single inheritance; meaning is a class can be extended from only one single class using the keyword 'extended'.
But still we can achieve using some mechanism Trait or Interface
xxxxxxxxxx
<?php
class GrandFather {
public function FunGrandFather() {
echo "I am from GrandFather Class"."\n";
}
}
interface IFather {
public function FunIFather();
}
class GrandSonClass extends GrandFather implements IFather {
function FunIFather() {
echo "I am in interface"."\n";
}
public function FunGrandSon() {
echo "I am in inherited GrandSonClass class "."\n";
}
}
$obj = new GrandSonClass();
$obj->FunGrandFather();
$obj->FunIFather();
$obj->FunGrandSon();
?>