PHP Inheritance
Reusing code through parent-child relationships
🌳 What is Inheritance?
Inheritance allows a class to inherit properties and methods from another class. The child class gets all features from the parent class and can add its own unique features, promoting code reuse and organization.
<?php
// Parent class
class Vehicle {
public $brand;
public function honk() {
return "Beep beep!";
}
}
// Child class inherits from Vehicle
class Car extends Vehicle {
public $model;
public function drive() {
return "$this->brand $this->model is driving!";
}
}
$car = new Car();
$car->brand = "Toyota";
$car->model = "Camry";
echo $car->honk(); // Inherited method
echo $car->drive(); // Own method
?>
Output:
Beep beep!
Toyota Camry is driving!
Inheritance Concepts
Parent Class
Base class that is inherited from
class Animal {
public $name;
}
Child Class
Class that inherits from parent
class Dog extends Animal {
public $breed;
}
Code Reuse
Inherit properties and methods
// Child gets parent's features
$dog->name = "Max";
Extension
Add new features to child class
public function bark() {
return "Woof!";
}
🔹 Basic Inheritance
Use the
extends
keyword to create a child class that inherits from a parent class. The child automatically gets all public and protected members.
<?php
class Person {
public $name;
public $age;
public function introduce() {
return "Hi, I'm $this->name, $this->age years old.";
}
}
class Student extends Person {
public $studentId;
public $grade;
public function study() {
return "$this->name is studying.";
}
}
$student = new Student();
$student->name = "Alice";
$student->age = 20;
$student->studentId = "S12345";
$student->grade = "A";
echo $student->introduce(); // Inherited method
echo $student->study(); // Own method
?>
Output:
Hi, I'm Alice, 20 years old.
Alice is studying.
🔹 Overriding Methods
Child classes can override parent methods by defining a method with the same name. This allows customizing inherited behavior while keeping the same interface.
<?php
class Animal {
public $name;
public function makeSound() {
return "Some generic sound";
}
public function sleep() {
return "$this->name is sleeping.";
}
}
class Cat extends Animal {
// Override parent method
public function makeSound() {
return "Meow!";
}
// Inherited sleep() method still works
}
class Dog extends Animal {
// Override parent method
public function makeSound() {
return "Woof!";
}
}
$cat = new Cat();
$cat->name = "Whiskers";
echo $cat->makeSound(); // Overridden: Meow!
echo $cat->sleep(); // Inherited: Whiskers is sleeping.
$dog = new Dog();
$dog->name = "Buddy";
echo $dog->makeSound(); // Overridden: Woof!
?>
Output:
Meow!
Whiskers is sleeping.
Woof!
🔹 Using parent:: Keyword
The
parent::
keyword allows child classes to call parent class methods, even when overriding them. This is useful for extending rather than replacing parent functionality.
<?php
class Shape {
protected $name;
public function __construct($name) {
$this->name = $name;
echo "Shape created: $name
";
}
public function describe() {
return "This is a $this->name";
}
}
class Circle extends Shape {
public $radius;
public function __construct($name, $radius) {
parent::__construct($name); // Call parent constructor
$this->radius = $radius;
echo "Radius set to: $radius
";
}
public function describe() {
$parentDesc = parent::describe(); // Call parent method
return "$parentDesc with radius $this->radius";
}
}
$circle = new Circle("Circle", 5);
echo $circle->describe();
?>
Output:
Shape created: Circle
Radius set to: 5
This is a Circle with radius 5
🔹 Protected Members in Inheritance
Protected properties and methods are accessible in child classes but not from outside. This provides controlled access for inheritance while maintaining encapsulation.
<?php
class BankAccount {
protected $balance;
protected $accountHolder;
public function __construct($holder, $initialBalance) {
$this->accountHolder = $holder;
$this->balance = $initialBalance;
}
protected function updateBalance($amount) {
$this->balance += $amount;
}
public function getBalance() {
return "Balance: $$this->balance";
}
}
class SavingsAccount extends BankAccount {
private $interestRate = 0.05;
public function addInterest() {
$interest = $this->balance * $this->interestRate;
$this->updateBalance($interest); // Can use protected method
return "Interest added: $$interest";
}
public function getAccountInfo() {
// Can access protected properties
return "$this->accountHolder's account: $$this->balance";
}
}
$savings = new SavingsAccount("John", 1000);
echo $savings->getBalance();
echo $savings->addInterest();
echo $savings->getAccountInfo();
?>
Output:
Balance: $1000
Interest added: $50
John's account: $1050
🔹 Practical Example: Multi-Level Inheritance
Inheritance can span multiple levels, creating a hierarchy of classes. Each level can add more specific features while inheriting from its parent.
<?php
// Level 1: Base class
class Employee {
public $name;
public $salary;
public function __construct($name, $salary) {
$this->name = $name;
$this->salary = $salary;
}
public function getDetails() {
return "$this->name - Salary: $$this->salary";
}
}
// Level 2: Inherits from Employee
class Manager extends Employee {
public $department;
public function __construct($name, $salary, $department) {
parent::__construct($name, $salary);
$this->department = $department;
}
public function getDetails() {
return parent::getDetails() . " - Dept: $this->department";
}
}
// Level 3: Inherits from Manager
class Director extends Manager {
public $bonus;
public function __construct($name, $salary, $department, $bonus) {
parent::__construct($name, $salary, $department);
$this->bonus = $bonus;
}
public function getDetails() {
return parent::getDetails() . " - Bonus: $$this->bonus";
}
public function getTotalCompensation() {
return $this->salary + $this->bonus;
}
}
$director = new Director("Alice", 100000, "IT", 20000);
echo $director->getDetails();
echo "Total: $" . $director->getTotalCompensation();
?>
Output:
Alice - Salary: $100000 - Dept: IT - Bonus: $20000
Total: $120000