1. Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). In PHP, inheritance allows one class to use the properties and methods of another class.
The class that is inherited from is called the Parent Class (Base Class), and the class that inherits the properties and methods is called the Child Class (Derived Class).
In PHP, inheritance is implemented using the extends keyword.
What is IS-A relationship?
In this image, Indra Gandhi is a Politician and a Daughter. So this is called an Is a relationship.
- Code reusability
- Better code organization
- Easier maintenance
- Faster development
2. Syntax
The basic syntax of PHP inheritance is very simple. A child class uses the extends keyword to inherit from a parent class.
class ParentClass {
// properties and methods
}
class ChildClass extends ParentClass {
// additional properties and methods
}
Here:
- ParentClass contains common functionality.
- ChildClass inherits all public and protected members of ParentClass.
- The child class can also add its own properties and methods.
Private members of the parent class cannot be accessed directly by the child class.
3. Example
Below is a simple example to understand PHP inheritance.
class Animal {
public function sound() {
echo "Animals make sounds";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog barks";
}
}
$dog = new Dog();
$dog->sound();
echo "<br>";
$dog->bark();
In this example, the Dog class inherits from the Animal class.
Output:
Dog barks
4. Real-world Example
Let’s take a practical example related to employees in a company.
A company may have many types of employees such as Manager, Developer, and Designer. All employees share common details like name and salary.
Instead of creating these properties in every class, we create a base Employee class and then inherit it.
class Employee {
public $name;
public $salary;
public function getEmployeeInfo() {
echo "Employee Name: " . $this->name . "
";
echo "Salary: " . $this->salary . "
";
}
}
class Developer extends Employee {
public $language;
public function getDeveloperInfo() {
echo "Programming Language: " . $this->language;
}
}
$dev = new Developer();
$dev->name = "John";
$dev->salary = 50000;
$dev->language = "PHP";
$dev->getEmployeeInfo();
$dev->getDeveloperInfo();
This example shows how a child class can reuse the properties and methods of a parent class.
Output:
Salary: 50000
Programming Language: PHP
5. Type of Inheritance
There are mainly 3 types of Inheritance in Php.
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
two other inheritance used through Interface.
- Multiple Inheritance
- Hybrid Inheritance
Why Multiple inheritance is not support PHP language?
Without Interface, Multiple inheritance is not support Php language.
Without Interface Multiple inheritance program.
6. Conclusion
PHP inheritance is a powerful feature of Object-Oriented Programming that allows one class to reuse the properties and methods of another class.
By using inheritance, developers can build a hierarchical relationship between classes and create scalable applications. The extends keyword makes it easy to inherit functionality from a parent class while allowing the child class to add its own behavior.
PHP Inheritance – Interview Questions
Q 1: What is inheritance in PHP?
Q 2: Keyword used for inheritance?
Q 3: Can PHP support multiple inheritance?
Q 4: Can child class override methods?
Q 5: Why use inheritance?
PHP Inheritance – Objective Questions (MCQs)
Q1. Inheritance allows a class to ______.
Q2. The class that inherits is called ______.
Q3. Which keyword is used for inheritance in PHP?
Q4. Private members of parent class are ______ accessible in child class.
Q5. Inheritance helps in ______.