Inheritance is one of the key features of Object-Oriented Programming (OOP) in PHP. It allows a class (child class) to inherit properties and methods from another class (parent class). This promotes code reusability and a hierarchical class structure.
🔹 What is Inheritance?
Inheritance allows us to create a new class from an existing class. The new class (child class) inherits all the public and protected properties and methods of the parent class.
Syntax
phpCopyEditclass ParentClass {
// Properties & Methods
}
class ChildClass extends ParentClass {
// Additional Properties & Methods
}
📝 Note: A child class cannot inherit private properties and methods of a parent class.
🔹 Types of Inheritance in PHP
PHP supports single-level inheritance and multilevel inheritance but does not support multiple inheritance (i.e., a class cannot inherit from more than one class directly).
1️⃣ Single-Level Inheritance
A single child class extends a parent class.
Example:
class Animal {
public function sound() {
echo "Animals make sounds!";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog barks!";
}
}
$dog = new Dog();
$dog->sound(); // Output: Animals make sounds!
$dog->bark(); // Output: Dog barks!
2️⃣ Multilevel Inheritance
A child class extends another child class.
Example:
class GrandParent {
public function familyName() {
echo "Smith Family";
}
}
class ParentClass extends GrandParent {
public function parentMethod() {
echo "Parent Method Called";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
echo "Child Method Called";
}
}
$obj = new ChildClass();
$obj->familyName(); // Output: Smith Family
$obj->parentMethod(); // Output: Parent Method Called
$obj->childMethod(); // Output: Child Method Called
🔹 Using protected
Access Modifier
protected
properties and methods cannot be accessed directly outside the class.- They can be accessed inside the child class.
Example:
class Car {
protected $brand = "Toyota";
protected function getBrand() {
return $this->brand;
}
}
class Model extends Car {
public function showBrand() {
return $this->getBrand();
}
}
$model = new Model();
echo $model->showBrand(); // Output: Toyota
🔹 Overriding Methods in Child Class
A child class can redefine a method inherited from the parent class. This is called method overriding.
Example:
class ParentClass {
public function show() {
echo "Hello from Parent Class!";
}
}
class ChildClass extends ParentClass {
public function show() {
echo "Hello from Child Class!";
}
}
$obj = new ChildClass();
$obj->show(); // Output: Hello from Child Class!
📝 Note: If a method is overridden in the child class, it replaces the parent method.
🔹 Calling Parent Class Method using parent::
To access the parent class method inside the child class, use the parent::
keyword.
Example:
class ParentClass {
public function message() {
echo "Hello from Parent!";
}
}
class ChildClass extends ParentClass {
public function message() {
parent::message(); // Call Parent Method
echo " Hello from Child!";
}
}
$obj = new ChildClass();
$obj->message();
// Output: Hello from Parent! Hello from Child
🔹 Constructor in Inheritance
A child class can also inherit a constructor from the parent class. If both the parent and child classes have constructors, the child class constructor will override the parent’s constructor.
Example:
class ParentClass {
public function __construct() {
echo "Parent Constructor Called\n";
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct(); // Call Parent Constructor
echo "Child Constructor Called";
}
}
$obj = new ChildClass();
// Output:
// Parent Constructor Called
// Child Constructor Called
🔹 Preventing Inheritance using final
Keyword
If you don’t want a class to be inherited, use the final
keyword.
Example:
final class FinalClass {
public function show() {
echo "Cannot be inherited";
}
}
// class ChildClass extends FinalClass {} // ❌ Error: Cannot extend final class
Similarly, you can prevent method overriding by using final
on methods.
class ParentClass {
final public function show() {
echo "Cannot be overridden";
}
}
class ChildClass extends ParentClass {
// public function show() {} // ❌ Error: Cannot override final method
}
🔹 Abstract Class and Inheritance
Abstract classes cannot be instantiated and must be inherited.
Example:
abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() {
echo "Woof! Woof!";
}
}
$dog = new Dog();
$dog->makeSound(); // Output: Woof! Woof!
📝 Note: Any child class must define the abstract method of the parent class.
🔹 Interfaces and Multiple Inheritance Alternative
PHP does not support multiple inheritance, but you can achieve it using interfaces.
Example:
interface A {
public function methodA();
}
interface B {
public function methodB();
}
class C implements A, B {
public function methodA() {
echo "Method A\n";
}
public function methodB() {
echo "Method B";
}
}
$obj = new C();
$obj->methodA(); // Output: Method A
$obj->methodB(); // Output: Method B
🔹 Conclusion
Inheritance in PHP is a powerful feature that allows: ✅ Code Reusability
✅ Method Overriding
✅ Hierarchical Class Structure
✅ Encapsulation using protected
✅ Use of parent::
to call parent methods
Would you like a video tutorial or need help with a real-world example? 🚀