Inheritance and Interfaces
Extending classes, overriding methods, and defining contracts with interfaces.
2 min read
Inheritance lets one class build on another; interfaces let unrelated classes promise to support the same set of methods.
Extending a class
<?php
class Animal {
public function __construct(protected string $name) {}
public function makeSound(): string {
return "...";
}
}
class Dog extends Animal {
public function makeSound(): string {
return "{$this->name} says Woof!";
}
}
$dog = new Dog("Rex");
echo $dog->makeSound(); // "Rex says Woof!"extends makes Dog a subclass of Animal, inheriting its constructor and properties automatically. makeSound() is overridden — Dog's version replaces Animal's for any Dog instance, a mechanism called polymorphism: code that just calls $animal->makeSound() gets the right behavior regardless of which subclass $animal actually is.
Calling the parent's implementation
<?php
class Employee {
public function __construct(protected string $name, protected float $baseSalary) {}
public function getSalary(): float {
return $this->baseSalary;
}
}
class Manager extends Employee {
public function __construct(string $name, float $baseSalary, private float $bonus) {
parent::__construct($name, $baseSalary);
}
public function getSalary(): float {
return parent::getSalary() + $this->bonus;
}
}parent:: calls the parent class's version of a method (or its constructor) explicitly — useful when a subclass wants to extend behavior rather than fully replace it, as Manager::getSalary() does here by adding a bonus on top of the inherited calculation.
Abstract classes
<?php
abstract class Shape {
abstract public function area(): float;
public function describe(): string {
return "Area: " . $this->area();
}
}
class Circle extends Shape {
public function __construct(private float $radius) {}
public function area(): float {
return M_PI * $this->radius ** 2;
}
}An abstract class can't be instantiated directly (new Shape() is an error) and can declare abstract methods with no body — every concrete subclass is required to implement them. describe() is a normal, inherited method that works for any subclass, since it only depends on area() existing, not on how each shape calculates it.
Interfaces: a contract without implementation
<?php
interface Payable {
public function calculatePayment(): float;
}
class Freelancer implements Payable {
public function __construct(private float $hourlyRate, private int $hours) {}
public function calculatePayment(): float {
return $this->hourlyRate * $this->hours;
}
}
function processPayment(Payable $entity): void {
echo "Paying: " . $entity->calculatePayment();
}An interface declares method signatures with no implementation at all — a class implements it as a promise that it provides those methods. Unlike extends (single inheritance only — a class can extend just one parent), a class can implements several interfaces at once, which is how PHP achieves something like multiple inheritance for behavior contracts without the complexity of true multiple class inheritance.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.