ADVERTISEMENT
ADVERTISEMENT

Types of Inheritance in PHP with Examples

Inheritance in PHP is an Object-Oriented Programming (OOP) concept that allows a child class to reuse the properties and methods of a parent class. There are four types of inheritance in PHP. This improves code reusability and helps in building structured applications. Learn about the various advantages of  inheritance in php.

Advantages of Inheritance in PHP

  • Code Reusability – Allows child classes to reuse methods and properties from the parent class, reducing redundancy.
  • Better Code Organization – Helps in structuring the code by creating relationships between classes.
  • Easy Maintenance – Changes made in the parent class automatically reflect in child classes, making updates easier.
  • Extensibility – Enables adding new functionalities in child classes without modifying the parent class.
  • Improved Readability – Keeps the code clean and easier to understand by following a hierarchical structure.
  • Encapsulation Support – Protects data using access modifiers like public, private, and protected.

Types of Inheritance in PHP 

There are four types of inheritance in PHP. 

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance Using Traits

1. Single Inheritance in PHP

Single inheritance in PHP allows a child class to inherit properties and methods from a single parent class. This helps in code reusability by enabling the child class to use the existing functionality of the parent class. The child class can also have its own additional methods. It is implemented using the extends keyword.

Example:

class Student {
    public function study() {
        echo "Student is studying.";
    }
}

class CollegeStudent extends Student {
    public function attendLecture() {
        echo "College student is attending a lecture.";
    }
}

$obj = new CollegeStudent();
$obj->study(); // Output: Student is studying.
$obj->attendLecture(); // Output: College student is attending a lecture.

.CollegeStudent inherits the study() method from Student and also has its own method attendLecture().

2. Multilevel Inheritance in PHP

Multilevel inheritance in PHP occurs when a class inherits from another child class, creating a chain of inheritance. The properties and methods of the top-level parent class are passed down to multiple levels. This helps in building a structured hierarchy where each child class can extend the functionality of its parent. It is implemented using multiple extends keywords in a sequence.

Example:

class Person {
    public function getName() {
        echo "This is a person.";
    }
}

class Student extends Person {
    public function getStudentID() {
        echo "Student ID is 123.";
    }
}

class SchoolStudent extends Student {
    public function getSchoolName() {
        echo "School name is ABC High School.";
    }
}

$obj = new SchoolStudent();
$obj->getName(); // Output: This is a person.
$obj->getStudentID(); // Output: Student ID is 123.
$obj->getSchoolName(); // Output: School name is ABC High School.

SchoolStudent inherits methods from both Person and Student, allowing access to all three methods.

3. Hierarchical Inheritance in PHP

Hierarchical inheritance in PHP occurs when multiple child classes inherit from the same parent class. Each child class gets access to the parent class’s properties and methods. This allows code reusability while enabling each child class to have its own unique features. It is implemented using the extends keyword for multiple child classes.

Example:

class Student {
    public function getDetails() {
        echo "This is a student.";
    }
}

class CollegeStudent extends Student {
    public function getCollegeName() {
        echo "College name is XYZ University.";
    }
}

class SchoolStudent extends Student {
    public function getGrade() {
        echo "Student is in 10th grade.";
    }
}

$obj1 = new CollegeStudent();
$obj1->getDetails(); // Output: This is a student.
$obj1->getCollegeName(); // Output: College name is XYZ University.

$obj2 = new SchoolStudent();
$obj2->getDetails(); // Output: This is a student.
$obj2->getGrade(); // Output: Student is in 10th grade.

Both CollegeStudent and SchoolStudent inherit from Student and have their own additional methods.

4. Multiple Inheritance Using Traits in PHP

Multiple inheritance using traits in PHP allows a class to inherit methods from multiple sources using trait. Since PHP does not support multiple inheritance directly, traits help reuse code from different traits in a single class. A class can use multiple traits with the use keyword.

Example:

trait Exam {
    public function takeExam() {
        echo "Student is taking an exam.";
    }
}

trait Sports {
    public function playSport() {
        echo "Student is playing a sport.";
    }
}

class Student {
    use Exam, Sports;

    public function study() {
        echo "Student is studying.";
    }
}

$obj = new Student();
$obj->study(); // Output: Student is studying.
$obj->takeExam(); // Output: Student is taking an exam.
$obj->playSport(); // Output: Student is playing a sport.

The Student class uses both Exam and Sports traits, allowing it to access methods from both.


ADVERTISEMENT

ADVERTISEMENT