PHP Inheritance Tutorial
markdown
Inheritance is one of the most powerful features of Object-Oriented Programming (OOP). It allows a class (child class) to inherit properties and methods from another class (parent class). This helps you write cleaner, reusable, and organized code.
In this tutorial, you’ll learn:
What inheritance is
How inheritance works in PHP
Types of inheritance PHP supports
Method overriding
Using parent:: to access parent methods
Real-world examples
Inheritance allows one class to use the properties and methods of another class.
You use the extends keyword in PHP:
class ChildClass extends ParentClass {
}
class Animal {
public function eat() {
echo "Animal is eating";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog is barking";
}
}
$dog = new Dog();
$dog->eat(); // inherited from Animal
$dog->bark(); // Dog’s own method
Animal is eating
Dog is barking
class Vehicle {
public $brand = "Tata";
public function showBrand() {
echo "Brand: " . $this->brand;
}
}
class Car extends Vehicle {
}
$c = new Car();
$c->showBrand();
A child class can redefine a parent method.
class Employee {
public function role() {
echo "General Employee";
}
}
class Manager extends Employee {
public function role() {
echo "Manager"; // overriding
}
}
$mgr = new Manager();
$mgr->role();
parent:: to Access Parent MethodsYou can call the parent’s method from the overridden child method.
class Student {
public function details() {
echo "Student details";
}
}
class CollegeStudent extends Student {
public function details() {
parent::details(); // call parent method
echo " (College Level)";
}
}
$cs = new CollegeStudent();
$cs->details();
Student details (College Level)
If the parent class has a constructor, the child can call it using parent::__construct().
class User {
public function __construct() {
echo "User created";
}
}
class Admin extends User {
public function __construct() {
parent::__construct();
echo " and Admin initialized";
}
}
$a = new Admin();
User created and Admin initialized
Protected properties/methods can be accessed by child classes.
class Account {
protected $balance = 1000;
}
class SavingsAccount extends Account {
public function showBalance() {
echo $this->balance; // allowed
}
}
$sa = new SavingsAccount();
$sa->showBalance();
PHP does not support multiple inheritance:
class A {}
class B {}
class C extends A, B {} // ❌ Not allowed
Instead, PHP provides traits for reusable code across classes.
class User {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function getRole() {
return "User";
}
}
class Admin extends User {
public function getRole() {
return "Admin";
}
}
class Editor extends User {
public function getRole() {
return "Editor";
}
}
$admin = new Admin("Shubham");
echo $admin->name . " - " . $admin->getRole();
You learned how inheritance works in PHP:
Child classes extend parent classes
Methods and properties can be reused
Overriding allows custom behavior
parent:: calls parent methods
PHP supports single inheritance only
Inheritance helps reduce code repetition and improves scalability.
Learn PHP inheritance with simple examples. Understand extends, method overriding, parent keyword, and real-world OOP examples.
php inheritance tutorial, php extends class, php parent method, php oop inheritance, php overriding
"PHP inheritance tutorial with class and object examples"