Lessons → PHP OOP → Lesson 3

Inheritance

PHP
⏱ 22 min read📖 IntermediateNot completed

Inheritance lets a class inherit properties and methods from another class. This promotes code reuse and creates a hierarchy of related classes.

Basic Inheritance

PHP
<?php
// Parent class
class Animal {
    public string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function eat(): void {
        echo "{$this->name} is eating.\n";
    }

    public function sleep(): void {
        echo "{$this->name} is sleeping.\n";
    }
}

// Child class inherits from Animal
class Dog extends Animal {
    public function bark(): void {
        echo "{$this->name} says: Woof!\n";
    }
}

class Cat extends Animal {
    public function meow(): void {
        echo "{$this->name} says: Meow!\n";
    }
}

$dog = new Dog("Rex");
$dog->eat();    // Rex is eating. (inherited)
$dog->bark();   // Rex says: Woof! (own method)

$cat = new Cat("Whiskers");
$cat->sleep();  // Whiskers is sleeping. (inherited)
$cat->meow();   // Whiskers says: Meow!
?>

Method Overriding

PHP
<?php
class Shape {
    public function area(): float {
        return 0;
    }

    public function describe(): string {
        return "I am a shape with area: " . $this->area();
    }
}

class Circle extends Shape {
    public function __construct(private float $radius) {}

    // Override parent method
    public function area(): float {
        return M_PI * $this->radius ** 2;
    }
}

class Rectangle extends Shape {
    public function __construct(
        private float $width,
        private float $height
    ) {}

    public function area(): float {
        return $this->width * $this->height;
    }
}

$circle = new Circle(5);
echo $circle->area();      // 78.54...
echo $circle->describe();  // I am a shape with area: 78.54...

$rect = new Rectangle(4, 6);
echo $rect->area();  // 24
?>

parent:: keyword

PHP
<?php
class Vehicle {
    public function __construct(
        public string $brand,
        public int $year
    ) {}

    public function describe(): string {
        return "{$this->year} {$this->brand}";
    }
}

class ElectricCar extends Vehicle {
    public function __construct(
        string $brand,
        int $year,
        public int $range  // km per charge
    ) {
        parent::__construct($brand, $year);  // Call parent constructor
    }

    public function describe(): string {
        return parent::describe() . " (Electric, {$this->range}km range)";
    }
}

$tesla = new ElectricCar("Tesla", 2023, 500);
echo $tesla->describe();
// 2023 Tesla (Electric, 500km range)
?>

final keyword

PHP
<?php
// final class — cannot be extended
final class Singleton {
    private static ?self $instance = null;

    public static function getInstance(): self {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}

class Base {
    // final method — cannot be overridden
    final public function identity(): string {
        return "I am Base";
    }
}
?>
💡
Laravel uses inheritance everywhere! Your controllers extend Controller, models extend Model, middleware implements Middleware. Understanding extends/parent is crucial for Laravel.
← Previous Lesson Next Lesson →
🧠

Test your knowledge!

Take a quiz to reinforce what you learned.

Take Quiz →