Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Inheritance & Interfaces
PHP Intermediate FREE

Inheritance & Interfaces

Lesson 2 of 17 Intermediate Interactive

Inheritance lets you create new classes based on existing ones. Interfaces define contracts that classes must follow.

Inheritance

  • extends — A child class inherits properties and methods from a parent class
  • parent::__construct() — Call the parent constructor
  • Child classes can override parent methods

Abstract Classes

A class that cannot be instantiated directly — it serves as a base class. Abstract methods must be implemented by child classes.

Interfaces

An interface defines methods that a class must implement. A class can implement multiple interfaces using implements.

Tip: Use interfaces when you need to define a contract that multiple unrelated classes must follow.

Syntax

PHP
<?php
// Inheritance

class Animal {
    public string $name;
    public function __construct(string $name) {
        $this->name = $name;
    }
    public function speak(): string {
        return "...";
    }
}

class Dog extends Animal {
    public function speak(): string {
        return "Woof!";
    }
}

// Abstract class

abstract class Shape {
    abstract public function area(): float;
}

class Circle extends Shape {
    public function __construct(private float $radius) {}
    public function area(): float {
        return M_PI * $this->radius ** 2;
    }
}

// Interface

interface Printable {
    public function toString(): string;
}

class Invoice implements Printable {
    public function toString(): string {
        return "Invoice #123";
    }
}
?>
PHP Inheritance and Interfaces
PHP
<?php
interface Shape {
    public function area(): float;
}

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

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

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

    public function area(): float {
        return pi() * $this->radius ** 2;
    }
}

// Inheritance
class Animal {
    public function speak(): string {
        return "Some sound";
    }
}

class Dog extends Animal {
    public function speak(): string {
        return "Woof!";
    }
}

$shapes = [new Rectangle(4, 5), new Circle(2)];
foreach ($shapes as $shape) {
    echo "Area: " . $shape->area() . "
";
}

echo (new Dog())->speak() . "
";
?>

Practice

1
Exercise

Create an abstract Database class with connect() and query() abstract methods, then create a MySQL implementation.

Answer
<?php
abstract class Database {
    abstract public function connect(): bool;
    abstract public function query(string $sql): array;
}

class MySQLDatabase extends Database {
    public function connect(): bool {
        echo "Connected to MySQL<br>";
        return true;
    }
    public function query(string $sql): array {
        return ["result" => "data"];
    }
}

$db = new MySQLDatabase();
$db->connect();
?>
2
Exercise

Create an interface Loggable with a log() method and implement it in two different classes.

Answer
<?php
interface Loggable {
    public function log(): string;
}

class Error implements Loggable {
    public function log(): string { return "[ERROR] Something failed"; }
}

class Warning implements Loggable {
    public function log(): string { return "[WARNING] Low disk space"; }
}

echo (new Error())->log() . "<br>";
echo (new Warning())->log();
?>

Quick Quiz

1

What keyword is used for inheritance in PHP?

The extends keyword makes a child class inherit from a parent class.

2

Can a class implement multiple interfaces?

PHP supports multiple interface implementation: class Foo implements Interface1, Interface2

3

What is an abstract class?

Abstract classes cannot be instantiated with new — they serve as base classes for inheritance.

Interview Questions

Use an interface when you need to define a contract that unrelated classes must follow (e.g., Loggable, Serializable). Use an abstract class when classes share common code and you want to provide default implementations. A class can implement multiple interfaces but extend only one abstract class.

The Liskov Substitution Principle states that objects of a child class should be able to replace objects of the parent class without breaking the program. In practice, this means child classes should maintain the same behavior contracts as parent classes — if a parent method returns a string, the child override should also return a string.