Master PHP Inheritance: Single, Multi‑Level, and Hierarchical Explained
This tutorial explains PHP inheritance concepts—including single, multi‑level, and hierarchical inheritance—showing how to reuse and extend code with practical examples of method overriding, the parent keyword, constructors, interfaces, and access modifiers.
PHP supports single inheritance, which improves code structure and allows methods and properties to be shared between classes.
Why Use Inheritance in PHP
Inheritance lets a subclass reuse a parent class’s code, reducing redundancy and making maintenance easier. It helps organize similar functionality in a parent class while allowing subclasses to extend or customize behavior. Updating a shared method in the parent automatically propagates to all subclasses.
PHP Inheritance Types
Single inheritance: a subclass inherits from one parent class.
Multi‑level inheritance: a class inherits from a subclass that already inherits from another class.
Hierarchical inheritance: multiple subclasses inherit from the same parent class.
Basic Example – Single Inheritance
class Gibson {
public function run() {
echo "Running device";
}
}
class AirCondition extends Gibson {
public function run() {
echo "Cooling room";
}
}
$ac = new AirCondition();
$ac->run();Output:
Cooling roomMulti‑Level Inheritance Example
class Device {
public function start() {
echo "Device started";
}
}
class Gibson extends Device {
public function run() {
echo "Running device";
}
}
class AirCondition extends Gibson {
public function cool() {
echo "Cooling room";
}
}AirCondition can call start(), run() and cool().
Hierarchical Inheritance Example
class Gibson {
public function run() {
echo "Running device";
}
}
class AirCondition extends Gibson {
public function cool() {
echo "Cooling room";
}
}
class Heater extends Gibson {
public function heat() {
echo "Warming room";
}
}Both AirCondition and Heater inherit the run() method from Gibson.
Method Overriding
When a subclass provides a new implementation for an existing parent method, the subclass method overrides the parent’s version.
class Device {
public function start() {
echo "Device started";
}
}
class AirCondition extends Device {
public function start() {
echo "AirCondition started";
}
}
$device = new AirCondition();
$device->start();Output:
AirCondition startedUsing the parent Keyword
The parent keyword allows a subclass to call the overridden method in the parent class.
class Device {
public function start() {
echo "Device started";
}
}
class AirCondition extends Device {
public function start() {
echo "AirCondition started. ";
parent::start();
}
}
$device = new AirCondition();
$device->start();Output:
AirCondition started. Device started.Constructors and Inheritance
Constructors are not inherited automatically; a subclass must call the parent constructor explicitly if it defines its own.
class Device {
public function __construct() {
echo "Device constructor called
";
}
}
class AirCondition extends Device {
public function __construct() {
parent::__construct();
echo "AirCondition constructor called
";
}
}
$device = new AirCondition();Output shows both constructors being executed.
Single vs Multi‑Level Inheritance
class Country {
public function capital() {
echo "The capital city of this country.";
}
}
class China extends Country {
public function language() {
echo "The language spoken is Chinese.";
}
}China inherits capital() from Country.
class Country { /* ... */ }
class Asia extends Country {
public function continent() {
echo "This country is in Asia.";
}
}
class China extends Asia {
public function language() {
echo "The language spoken is Japanese.";
}
}China can access capital(), continent(), and language().
Inheritance vs Interfaces
Inheritance shares actual code, while interfaces only define method signatures that a class must implement.
interface Speak {
public function speak();
}
class Japan implements Speak {
public function speak() {
echo "Japan speaks Japanese.";
}
}Access Modifiers
Access modifiers control visibility of properties and methods:
public : accessible everywhere, including subclasses.
protected : accessible within the class and its subclasses.
private : accessible only within the defining class.
class Device {
public $name;
protected $brand;
private $serialNumber;
public function __construct($name, $brand, $serialNumber) {
$this->name = $name;
$this->brand = $brand;
$this->serialNumber = $serialNumber;
}
public function getName() { return $this->name; }
protected function getBrand() { return $this->brand; }
private function getSerialNumber() { return $this->serialNumber; }
}
class AirCondition extends Device {
public function displayInfo() {
echo $this->getName();
echo $this->getBrand();
// $this->getSerialNumber(); // not accessible
}
}
$ac = new AirCondition("Cooler", "Samsung", "12345");
$ac->displayInfo();Conclusion
Inheritance enables code reuse by allowing subclasses to inherit, extend, or override parent functionality. PHP supports single, multi‑level, and hierarchical inheritance, method overriding, the parent keyword, constructors, interfaces, and access modifiers, providing a flexible object‑oriented toolkit for backend development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
