← Back to PHP Course | Chapter 8: OOP | Lesson 8 of 14

PHP Interfaces

An interface is like a rule list that says 'anyone who wants to join must be able to do these things'. A class that signs up promises to have each of those abilities.
Syntax
php
interface InterfaceName {
    function method_name();
}

class ClassName implements InterfaceName, OtherInterface {
    function method_name() {
        // implementation
    }
}

What is an Interface?

An interface defines a contract of method signatures that any implementing class must provide, without specifying any implementation details itself — it says what a class must do, not how.

Example: What is an Interface?

php
<?php
// Define the interface `Shape` describing required methods
interface Shape {
    // Define the function `area` with no parameters
    function area();
}
// Define the class `Circle`, implementing `Shape`
class Circle implements Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `3.14 * 5 * 5` from this function
        return 3.14 * 5 * 5;
    }
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area()` to the output
echo $c->area();
?>

Implementing Interfaces

A class agrees to an interface's contract using the implements keyword, and PHP will produce a fatal error at runtime if the class fails to define every method the interface requires.

Example: Implementing Interfaces

php
<?php
// Define the interface `Shape` describing required methods
interface Shape {
    // Define the function `area` with no parameters
    function area();
}
// Define the class `Square`, implementing `Shape`
class Square implements Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `9` from this function
        return 9;
    }
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
// Print `$sq->area()` to the output
echo $sq->area();
?>

Multiple Interfaces

Unlike classes, PHP lets a single class implement multiple interfaces at once, which is how PHP achieves the flexibility of multiple inheritance without the ambiguity problems that true multiple class inheritance would create.

Example: Multiple Interfaces

php
<?php
// Define the interface `Swims` describing required methods
interface Swims {
    // Define the function `swim` with no parameters
    function swim();
}
// Define the interface `Flies` describing required methods
interface Flies {
    // Define the function `fly` with no parameters
    function fly();
}
// Define the class `Duck`, implementing `Swims, Flies`
class Duck implements Swims, Flies {
    // Define the function `swim` with no parameters
    function swim() { echo "Swimming\n"; }
    // Define the function `fly` with no parameters
    function fly() { echo "Flying\n"; }
}
// Create a new `Duck` instance, stored in `$duck`
$duck = new Duck();
$duck->swim();
$duck->fly();
?>

Interface Inheritance

Interfaces are especially valuable for decoupling code: a function that accepts a Countable interface, for instance, can work with any object that implements count() correctly, regardless of what concrete class it actually is.

Example: Interface Inheritance

php
<?php
// Define the class `Cart`, implementing `Countable`
class Cart implements Countable {
    // Declare the private property `$items`, set to `["a", "b", "c"]`
    private $items = ["a", "b", "c"];
    // Define the function `count` with no parameters
    function count() {
        // Return `count($this->items)` from this function
        return count($this->items);
    }
}
// Create a new `Cart` instance, stored in `$cart`
$cart = new Cart();
// Print `count($cart)` to the output
echo count($cart);
?>

Interfaces vs. Abstract Classes

PHP itself ships with several built-in interfaces like Iterator, ArrayAccess, and Countable that let your own custom objects behave like native arrays in foreach loops or with functions like count().

Example: Interfaces vs. Abstract Classes

php
<?php
// Define the class `Playlist`, implementing `Iterator`
class Playlist implements Iterator {
    // Declare the private property `$songs`, set to `["Song A", "Song B"]`
    private $songs = ["Song A", "Song B"];
    // Declare the private property `$position`, set to `0`
    private $position = 0;
    // Define the function `current` with no parameters
    function current() { return $this->songs[$this->position]; }
    // Define the function `key` with no parameters
    function key() { return $this->position; }
    // Define the function `next` with no parameters
    function next() { $this->position++; }
    // Define the function `rewind` with no parameters
    function rewind() { $this->position = 0; }
    // Define the function `valid` with no parameters
    function valid() { return isset($this->songs[$this->position]); }
}
// Loop over `new Playlist()`, binding each item to `$song`
foreach (new Playlist() as $song) {
    // Print `$song . "\n"` to the output
    echo $song . "\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not implementing every method of the interface, which is a fatal error.
  2. Declaring interface methods as anything other than public.
  3. Trying to put properties in an interface, when it can only declare methods and constants.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.