Menu
Coddy logo textTech

Implementing Interfaces

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

Now that you understand what interfaces are, let's see how to actually implement them. A class implements an interface using the implements keyword, and must provide concrete implementations for all methods declared in that interface.

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

class Document implements Printable {
    private $content;
    
    public function __construct($content) {
        $this->content = $content;
    }
    
    public function print() {
        return "Printing: " . $this->content;
    }
}

$doc = new Document("Hello World");
echo $doc->print();

Output:

Printing: Hello World

The Document class implements Printable by providing its own version of the print() method. If you forget to implement a required method, PHP will throw a fatal error.

Different classes can implement the same interface in their own way:

<?php
class Invoice implements Printable {
    private $amount;
    
    public function __construct($amount) {
        $this->amount = $amount;
    }
    
    public function print() {
        return "Invoice Total: $" . $this->amount;
    }
}

$invoice = new Invoice(150);
echo $invoice->print();

Output:

Invoice Total: $150

Both Document and Invoice fulfill the Printable contract, but each handles printing differently based on its purpose.

Key Point: Use implements to connect a class to an interface, then define all required methods. Each implementing class provides its own logic while guaranteeing the same method signatures exist.

challenge icon

Challenge

Easy

Let's build a messaging system that demonstrates how different classes can implement the same interface in their own unique ways.

You'll create three files that work together to handle different types of messages:

  • Messageable.php — Define the Messageable interface with two method signatures: compose($content) and deliver(). This interface establishes the contract that all message types must follow.
  • Email.php — Create an Email class that implements Messageable. Include the interface file at the top. The class should have a private $recipient property and a private $body property. The constructor accepts the recipient address. Implement compose($content) to store the content in $body and return "Email composed". Implement deliver() to return "Email to [recipient]: [body]".
  • SMS.php — Create an SMS class that also implements Messageable. Include the interface file at the top. The class should have a private $phoneNumber property and a private $text property. The constructor accepts the phone number. Implement compose($content) to store the content in $text and return "SMS composed". Implement deliver() to return "SMS to [phoneNumber]: [text]".

In main.php, include both the Email and SMS files. You'll receive three inputs: an email address, a phone number, and a message content. Create an Email object with the email address and an SMS object with the phone number. Call compose() with the message content on both objects, printing each result on its own line. Then call deliver() on both objects, printing each result on its own line. The order should be: email compose, SMS compose, email deliver, SMS deliver.

Both classes fulfill the same Messageable contract, but each handles composing and delivering messages according to its own purpose — emails show the recipient address while SMS messages show the phone number.

Cheat sheet

A class implements an interface using the implements keyword and must provide concrete implementations for all methods declared in that interface:

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

class Document implements Printable {
    private $content;
    
    public function __construct($content) {
        $this->content = $content;
    }
    
    public function print() {
        return "Printing: " . $this->content;
    }
}

Multiple classes can implement the same interface, each providing their own implementation:

<?php
class Invoice implements Printable {
    private $amount;
    
    public function __construct($amount) {
        $this->amount = $amount;
    }
    
    public function print() {
        return "Invoice Total: $" . $this->amount;
    }
}

If a class fails to implement all required methods from an interface, PHP will throw a fatal error.

Try it yourself

<?php
require_once 'Email.php';
require_once 'SMS.php';

// Read inputs
$emailAddress = trim(fgets(STDIN));
$phoneNumber = trim(fgets(STDIN));
$messageContent = trim(fgets(STDIN));

// TODO: Create an Email object with the email address

// TODO: Create an SMS object with the phone number

// TODO: Call compose() on both objects and print results
// Order: email compose, then SMS compose

// TODO: Call deliver() on both objects and print results
// Order: email deliver, then SMS deliver
?>
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