Menu
Coddy logo textTech

Multiple Interface Implement

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 29 of 91.

Unlike inheritance where a class can only extend one parent, PHP allows a class to implement multiple interfaces. This gives you the flexibility to combine different contracts, making your classes more versatile.

To implement multiple interfaces, separate them with commas after the implements keyword:

<?php
interface Printable {
    public function print();
}

interface Savable {
    public function save();
}

class Report implements Printable, Savable {
    private $title;
    
    public function __construct($title) {
        $this->title = $title;
    }
    
    public function print() {
        return "Printing: " . $this->title;
    }
    
    public function save() {
        return "Saving: " . $this->title;
    }
}

$report = new Report("Sales Report");
echo $report->print() . "\n";
echo $report->save();

Output:

Printing: Sales Report
Saving: Sales Report

The Report class fulfills both contracts by implementing all methods from Printable and Savable. You must provide implementations for every method declared in each interface.

This approach is powerful because it lets you build classes that satisfy multiple requirements without the constraints of single inheritance. A class can be printable, savable, exportable, and more, all at once.

Key Point: Use commas to implement multiple interfaces. Your class must provide concrete implementations for all methods from every interface it implements.

challenge icon

Challenge

Easy

Let's build a media file system that demonstrates how a single class can implement multiple interfaces to gain different capabilities.

You'll create four files that work together to handle media files that can be both played and shared:

  • Playable.php — Define a Playable interface with a single method signature: play(). This contract ensures any playable media can be started.
  • Shareable.php — Define a Shareable interface with a single method signature: share($platform). This contract ensures any shareable content can be distributed to different platforms.
  • Video.php — Create a Video class that implements both Playable and Shareable. Include both interface files at the top. The class should have a private $title property and a private $duration property (in minutes). The constructor accepts the title and duration. Implement play() to return "Playing video: [title] ([duration] min)". Implement share($platform) to return "Sharing [title] on [platform]".
  • main.php — Include the Video file. You'll receive three inputs: the video title, duration (as a string to convert to an integer), and a platform name. Create a Video object with the title and duration. Print the result of calling play() on the first line, then print the result of calling share() with the platform on the second line.

Your Video class fulfills two separate contracts simultaneously — it can be played like any media, and it can be shared to social platforms. This is the power of implementing multiple interfaces: a single class gains capabilities from different sources without being limited to a single inheritance chain.

Cheat sheet

A class can implement multiple interfaces by separating them with commas after the implements keyword:

<?php
interface Printable {
    public function print();
}

interface Savable {
    public function save();
}

class Report implements Printable, Savable {
    public function print() {
        return "Printing report";
    }
    
    public function save() {
        return "Saving report";
    }
}

The class must provide concrete implementations for all methods from every interface it implements. This allows a class to fulfill multiple contracts simultaneously without the constraints of single inheritance.

Try it yourself

<?php
// Include the Video file
require_once 'Video.php';

// Read inputs
$title = trim(fgets(STDIN));
$duration = intval(trim(fgets(STDIN)));
$platform = trim(fgets(STDIN));

// TODO: Create a Video object with the title and duration

// TODO: Print the result of calling play() on the first line

// TODO: Print the result of calling share() with the platform on the second line

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming