PHP Interfaces
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Not implementing every method of the interface, which is a fatal error.
- Declaring interface methods as anything other than public.
- Trying to put properties in an interface, when it can only declare methods and constants.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: