Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class.
In simple words, inheritance helps you reuse existing code instead of rewriting it. This makes your code cleaner, more structured, and easier to maintain.
In this tutorial, you will learn what inheritance is, how it works in PHP, and why it is useful in real-world applications.
What is Inheritance?
Inheritance allows a class to reuse properties and methods of another class.
- Parent class → base class
- Child class → derived class
- Child class uses
extendskeyword
This helps avoid code duplication.
Why Use Inheritance?
Inheritance helps you:
- Reuse existing code
- Avoid duplication
- Maintain clean structure
- Create logical class hierarchy
- Maintain large applications easily
- Follow DRY principle (Don’t Repeat Yourself)
Basic Inheritance Example
<?php
class User {
public $name;
public function setName($name) {
$this->name = $name;
}
}
?>
Creating a Child Class
<?php
class Admin extends User {
public function showRole() {
echo "Admin User";
}
}
?>
Using Parent & Child Class
<?php
$admin = new Admin();
$admin->setName("Divyesh");
echo $admin->name;
$admin->showRole();
?>
✔ Admin class uses methods from User
Method Overriding
A child class can override a parent method.
<?php
class User {
public function getRole() {
return "User";
}
}
class Admin extends User {
public function getRole() {
return "Admin";
}
}
?>
Overridden Method Output
<?php
$admin = new Admin();
echo $admin->getRole();
?>
Output:
Admin
Using parent:: Keyword
To access parent method inside child class:
<?php
class Admin extends User {
public function getRole() {
return parent::getRole() . " (Admin Access)";
}
}
?>
Inheritance with Access Modifiers
public→ accessible everywhereprotected→ accessible in child classprivate→ NOT accessible in child class
class User {
protected $email;
}
✔ Child class can access $email
Real-World Example
Let’s understand this with a simple example.
Suppose you have a class called Person.
<?php
class Person {
public $name; public function greet() {
echo "Hello, my name is " . $this->name;
}
}
?>
Now, you create a class called Student that extends Person.
<?php
class Student extends Person {
public $rollNumber; public function showRollNumber() {
echo "My roll number is " . $this->rollNumber;
}
}$student1 = new Student();
$student1->name = "Rahul";
$student1->rollNumber = 101;$student1->greet();
$student1->showRollNumber();
?>
What Happened Here?
Studentinherited thegreet()method fromPerson- We did not rewrite the greet method
- Code reuse happened automatically
This is the power of inheritance.
Using Protected with Inheritance
The protected access modifier works especially well with inheritance.
private→ cannot be accessed in child classprotected→ can be accessed in child class
Example:
<?php
class ParentClass {
protected $name = "Rahul";
}class ChildClass extends ParentClass {
public function showName() {
echo $this->name;
}
}$obj = new ChildClass();
$obj->showName();
?>
Here, the child class can access the protected property.
Important Points to Remember
- A class can extend only one parent class in PHP
- Inheritance creates an “is-a” relationship
- Child classes can add new methods
- Child classes can override parent methods
- Use protected when child classes need access
Limitations of Inheritance
- PHP supports single inheritance only
- A class can extend only one parent class
(Interfaces solve this — coming later)
Best Practices
- Keep parent class generic
- Avoid deep inheritance chains
- Prefer composition when possible (advanced topic)
Practical Task
- Create a class called Vehicle. Then add a method in it called start().
- Create another class called Car that extends Vehicle. Then add a method drive() in it.
- Create an object of Car can call both the methods.
- Observe how the child class can use methods from the parent class.
Summary
- Inheritance allows one class to inherit features from another class
- It is implemented using the
extendskeyword - It promotes code reuse and cleaner structure
- Child classes can override parent methods
- Protected members can be accessed in child classes
Inheritance is a powerful concept in OOP and is widely used in modern PHP applications.
In the next tutorial, we’ll learn about Encapsulation in PHP.
