PHP Inheritance
Inheritance is a core principle of object-oriented programming that allows a class to acquire properties and methods from another class. The class that is inherited from is called the parent class (or base class), and the class doing the inheriting is called the child class (or derived class). Inheritance promotes code reuse — common behavior is written once in the parent and shared automatically with all child classes.
Basic Inheritance with extends
<?php
class Animal {
public $name;
public $sound;
public function __construct($name, $sound) {
$this->name = $name;
$this->sound = $sound;
}
public function speak() {
echo $this->name . " says " . $this->sound . "!";
}
public function sleep() {
echo $this->name . " is sleeping.";
}
}
class Dog extends Animal {
public function fetch() {
echo $this->name . " fetches the ball!";
}
}
class Cat extends Animal {
public function purr() {
echo $this->name . " is purring.";
}
}
$dog = new Dog("Rex", "Woof");
$cat = new Cat("Whiskers", "Meow");
$dog->speak(); // Rex says Woof! (inherited from Animal)
$dog->fetch(); // Rex fetches the ball! (Dog's own method)
$cat->speak(); // Whiskers says Meow! (inherited from Animal)
$cat->purr(); // Whiskers is purring. (Cat's own method)
?>
Both Dog and Cat inherit $name, $sound, speak(), and sleep() from Animal without rewriting them.
Overriding Methods
A child class can override a parent's method by defining a method with the same name. The child's version replaces the parent's version for instances of that child class.
<?php
class Vehicle {
public function fuelType() {
return "Gasoline";
}
public function describe() {
echo "This vehicle runs on " . $this->fuelType() . ".";
}
}
class ElectricCar extends Vehicle {
// Override the parent's method
public function fuelType() {
return "Electricity";
}
}
class HybridCar extends Vehicle {
public function fuelType() {
return "Gasoline and Electricity";
}
}
$v = new Vehicle();
$e = new ElectricCar();
$h = new HybridCar();
$v->describe(); // This vehicle runs on Gasoline.
$e->describe(); // This vehicle runs on Electricity.
$h->describe(); // This vehicle runs on Gasoline and Electricity.
?>
Calling the Parent with parent::
When overriding a method, the child class can call the parent's original version using the parent:: keyword. This is useful when the child wants to extend the parent's behavior rather than completely replace it.
<?php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function introduce() {
echo "My name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
class Employee extends Person {
public $company;
public $position;
public function __construct($name, $age, $company, $position) {
parent::__construct($name, $age); // Call parent constructor
$this->company = $company;
$this->position = $position;
}
public function introduce() {
parent::introduce(); // Call parent's introduce()
echo " I work at " . $this->company . " as a " . $this->position . ".";
}
}
$person = new Person("Alice", 30);
$employee = new Employee("Bob", 35, "TechCorp", "Developer");
$person->introduce();
// My name is Alice and I am 30 years old.
$employee->introduce();
// My name is Bob and I am 35 years old. I work at TechCorp as a Developer.
?>
The final Keyword
Marking a class or method as final prevents it from being extended or overridden.
<?php
class BaseConfig {
public $debugMode = false;
final public function loadDefaults() {
// This method cannot be overridden in child classes
$this->debugMode = false;
}
}
final class DatabaseConfig extends BaseConfig {
// This class cannot be extended further
public $host = "localhost";
}
// class MyConfig extends DatabaseConfig {} // Fatal error - cannot extend final class
?>
Checking Class Relationships
<?php
class A {}
class B extends A {}
class C extends B {}
$c = new C();
var_dump($c instanceof C); // bool(true)
var_dump($c instanceof B); // bool(true) - C inherits from B
var_dump($c instanceof A); // bool(true) - C inherits from A through B
echo get_class($c); // C
echo get_parent_class($c); // B
?>
Multilevel Inheritance Example
<?php
class Shape {
public function area() {
return 0;
}
public function describe() {
echo "This shape has an area of " . $this->area() . ".";
}
}
class Rectangle extends Shape {
public function __construct(
private float $width,
private float $height
) {}
public function area(): float {
return $this->width * $this->height;
}
}
class Square extends Rectangle {
public function __construct(float $side) {
parent::__construct($side, $side);
}
}
$rect = new Rectangle(5, 8);
$square = new Square(4);
$rect->describe(); // This shape has an area of 40.
$square->describe(); // This shape has an area of 16.
?>
Key Points
- The
extendskeyword creates a child class that inherits all public and protected properties and methods from the parent. - Child classes can override parent methods by defining a method with the same name.
parent::calls the parent class's constructor or methods from within the child class.- PHP supports single inheritance only — a class can extend only one parent class.
- The
finalkeyword on a class prevents it from being extended; on a method, it prevents overriding. - The
instanceofoperator checks whether an object is an instance of a given class or any of its parent classes.
