Open In App

What is Inheritance in PHP?

Inheritance is a fundamental object-oriented programming concept in PHP where a class (subclass or child class) can inherit properties and methods from another class (superclass or parent class). It enables code reusability and promotes hierarchical relationships between classes.

Syntax:

// Parent class
class Animal {
public $species;

public function sound() {
return "Animal makes a sound.";
}
}

// Child class inheriting from Animal
class Dog extends Animal {
public function sound() {
return "Dog barks.";
}
}

Explanation: To implement inheritance in PHP, use the extends keyword followed by the name of the parent class. The child class inherits all public and protected properties and methods from the parent class.

Accessing Parent Class Members:

Constructor in Inheritance:

Single Inheritance:

Multiple Inheritance:

Article Tags :