Inheritance in PHP is a mechanism where a new class is derived from an existing class. The new class, called the child class or subclass, inherits all the properties and methods of the existing class, called the parent class or superclass. There are different types of inheritance in PHP, such as single inheritance, multiple inheritance, and hierarchical inheritance.
Table of Contents
Implementation of PHP Inheritance
To extend a class in PHP, you need to use the keyword “extends.” For example, if you have a class called “Animal,” and you want to create a new class called “Dog” that inherits from “Animal,” you can use the following code:
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function sound() {
echo "I am an animal";
}
}
class Dog extends Animal {
public function sound() {
echo "I am a dog";
}
}
$dog = new Dog("Fido");
echo $dog->name; // Output: Fido
echo $dog->sound(); // Output: I am a dog
Types of inheritance
In PHP, there are four types of inheritance: single, multiple, multilevel, and hierarchical.
Single Inheritance
Single inheritance is the most common type of inheritance in PHP, in which a subclass extends a single parent class.
class Vehicle {
public function start() {
echo "Vehicle started\n";
}
}
class Car extends Vehicle {
public function drive() {
echo "Car is being driven\n";
}
}
$car = new Car();
$car->start(); // Output: Vehicle started
$car->drive(); // Output: Car is being driven
Multiple Inheritance
Multiple inheritance allows a subclass to inherit properties and methods from multiple parent classes. However, PHP does not support multiple inheritance. Instead, you can use interfaces to achieve similar functionality.
interface Vehicle {
public function start();
}
interface Device {
public function turnOn();
}
class Car implements Vehicle, Device {
public function start() {
echo "Vehicle started\n";
}
public function turnOn() {
echo "Device turned on\n";
}
}
$car = new Car();
$car->start(); // Output: Vehicle started
$car->turnOn(); // Output: Device turned on
Multilevel Inheritance
Multilevel inheritance is when a subclass extends another subclass, which extends another class, and so on.
class Animal {
public function eat() {
echo "Animal is eating\n";
}
}
class Mammal extends Animal {
public function sleep() {
echo "Mammal is sleeping\n";
}
}
class Dog extends Mammal {
public function bark() {
echo "Dog is barking\n";
}
}
$dog = new Dog();
$dog->eat(); // Output: Animal is eating
$dog->sleep(); // Output: Mammal is sleeping
$dog->bark(); // Output: Dog is barking
Hierarchical Inheritance
Hierarchical inheritance is when multiple subclasses extend a single parent class.
class Shape {
public function draw() {
echo "Shape is being drawn\n";
}
}
class Circle extends Shape {
public function draw() {
echo "Circle is being drawn\n";
}
}
class Square extends Shape {
public function draw() {
echo "Square is being drawn\n";
}
}
$circle = new Circle();
$circle->draw(); // Output: Circle is being drawn
$square = new Square();
$square->draw(); // Output: Square is being drawn
Overriding and Extending Methods and Properties
In PHP inheritance, you can override methods and properties of the parent class in the child class. This means that the child class can have its own implementation of a method or property that has the same name as the one in the parent class. You can also extend methods and properties of the parent class in the child class, which means that the child class can use the methods and properties of the parent class in addition to its own.
class Car {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}
public function startEngine() {
echo "The $this->brand is starting its engine";
}
}
class ElectricCar extends Car {
public function startEngine() {
echo "The $this->brand is starting its electric engine";
}
}
$electricCar = new ElectricCar("Tesla");
$electricCar->startEngine(); // Output: The Tesla is starting its electric engine
Conclusion
Inheritance is a fundamental concept in OOP and is widely used in PHP programming. Understanding PHP inheritance and its importance can help you write more efficient and maintainable code. By following best practices for using PHP inheritance, you can create a well-structured and organized codebase.