Menu
Coddy logo textTech

Introduction to Interfaces

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

An interface defines a contract that classes must follow. Unlike abstract classes that can contain both abstract and concrete methods, an interface only declares method signatures without any implementation. Think of it as a promise: any class that implements an interface guarantees it will provide those specific methods.

You define an interface using the interface keyword:

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

This interface declares that any class implementing Printable must have a print() method. The interface doesn't care how the method works, only that it exists.

Here's a simple example showing the basic structure:

<?php
interface Drivable {
    public function start();
    public function stop();
}

Notice that interface methods have no body, just the signature ending with a semicolon. All methods in an interface are implicitly public, so you cannot use private or protected.

Interfaces are powerful because they allow unrelated classes to share common behavior. A Car and a Motorcycle might have nothing in common inheritance-wise, but both can implement Drivable to guarantee they have start() and stop() methods.

Key Point: An interface defines what methods a class must have, not how they should work. It establishes a contract that implementing classes must fulfill.

challenge icon

Challenge

Easy

Let's build a simple messaging system that introduces you to interfaces. You'll define an interface that establishes a contract for how messages should be handled, setting the foundation for the implementing classes you'll create in the next lesson.

You'll organize your code across two files:

  • Messageable.php — Define an interface called Messageable that declares two method signatures: compose($content) for creating a message, and deliver() for sending it. Remember, interface methods have no body — just the signature ending with a semicolon.
  • main.php — Include the Messageable file. To verify your interface is correctly defined, use PHP's interface_exists() function to check if Messageable exists. Then use the ReflectionClass to inspect your interface and list its methods. Create a reflection of your interface, get its methods using getMethods(), and print each method name on its own line.

Your output should display the two method names that your interface declares, each on a separate line, in the order you defined them.

This challenge focuses purely on defining an interface — the contract that future classes will need to fulfill. You're establishing what methods must exist, without worrying about how they'll work. That implementation comes in the next lesson!

Cheat sheet

An interface defines a contract that classes must follow. It only declares method signatures without any implementation.

Define an interface using the interface keyword:

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

Interface methods have no body, just the signature ending with a semicolon. All methods in an interface are implicitly public.

<?php
interface Drivable {
    public function start();
    public function stop();
}

Interfaces allow unrelated classes to share common behavior by guaranteeing they implement specific methods.

Try it yourself

<?php
// Include the Messageable interface file
require_once 'Messageable.php';

// TODO: Use interface_exists() to check if Messageable interface exists

// TODO: Create a ReflectionClass object for the Messageable interface

// TODO: Get the methods using getMethods()

// TODO: Loop through the methods and print each method name on its own 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