Summary: in this tutorial, you will learn how to use PHP interfaces that are one of the most important building blocks in PHP object-oriented programming.
Introduction to PHP interface
An interface allows you to specify a list of methods that a class must implement. To define an interface, you use the interface
keyword as follows:
1 2 3 4 | <?php interface IMyInterface{ // methods... } |
An interface consists of methods that contain no implementation. In other words, all methods of the interface are abstract methods. An interface can have its own constants. See the following syntax:
1 2 3 4 5 6 7 8 | <?php interface IMyInterface{ const INTERFACE_CONSTANT_1 = 1; const INTERFACE_CONSTANT_2 = 'a string'; public function method_1(); public function method_2(); } |
All the methods in the interface must have a public visibility level.
When we want to declare a new class that reuses properties and methods of an existing class, we say a subclass inherits from or extends a parent class. However, for the interfaces, we say a class implements an interface. A class can only inherit from one class whereas a class can implement one or more interfaces.
To implement an interface, you use the implements
operator as follows:
1 2 3 4 5 6 7 8 | class MyClass implements IMyInterface{ public function method_1(){ // method 1 implementation } public function method_2(){ // method 2 implementation } } |
When a class implements an interface, we also called it a concrete class. The concrete class must implement all methods the interface with the same names and signatures.
A class cannot implement two interfaces that have the same method name because it would end up with the method ambiguity.
Like a class, you can extend an interface using the extends
operator as follows:
1 2 3 4 5 6 7 | interface IReadable{ public function read(); } interface IWritable extends IReadable{ public function write(); } |
The IWritable
interface extends the IReadable
interface by adding one more abstract method named write()
.
Why should we use PHP interfaces?
There are several reasons that we should use interface in our PHP object-oriented programming:
- By implement an interface, the caller of the object need to care only about the object’s interface, not implementations of the object’s methods. Therefore the implementations can be changed without affecting the caller of the interface.
- An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
- An interface enables you to model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.
PHP interface example
In the following example, we will show you how to use the interface to make the system easier to extend and more flexible. Suppose, we have to create a logger that can log a message.

PHP Interface – ILogger Example
First, we create an interface called ILogger
as follows:
1 2 3 4 | <?php interface ILogger{ public function log($message); } |
Second, we can create a FileLogger
class that writes the log messages to a log file.
Let’s create the FileLogger class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php require_once 'ilogger.php'; class FileLogger implements ILogger{ private $handle; private $logFile; public function __construct($filename,$mode = 'a'){ $this->logFile = $filename; // open log file for append $this->handle = fopen($filename, $mode) or die('Could not open the log file'); } public function log($message){ $message = date("F j, Y, g:i a") . ': ' . $message . "\n"; fwrite($this->handle,$message); } public function __destruct(){ if($this->handle){ fclose($this->handle); } } } |
We can use the FileLogger
class as follows:
1 2 3 4 5 6 7 8 9 10 | <?php require_once 'classes/filelogger.php'; function testFilelogger(){ $logger = new FileLogger('./log.txt','w'); $logger->log('start using file logger'); $logger->log('another log message'); } testFilelogger(); |
We can add a database logger that implements ILogger
interface as follows:
For the sake of demonstration, we make the DBLogger
class as simple as possible.
1 2 3 4 5 6 7 8 9 | <?php require_once 'ilogger.php'; class DBLogger implements ILogger{ public function log($message){ echo sprintf("Log %s to the database\n",$message); } } |
We can easily add other kinds of loggers that implement the ILogger
interface without touching existing loggers.
The following code snippet demonstrates how to use multiple loggers at the same time using a single ILogger
interface:
1 2 3 4 5 6 7 8 9 10 11 | function testLoggers(){ $loggers = array( new FileLogger('./log.txt'), new DBLogger() ); foreach ($loggers as $logger) { $logger->log('Log message'); } } testLoggers(); |
In this tutorial, we have shown you how to use the PHP interface, which is one of the most important building blocks in PHP object-oriented programming.