What is an interface in PHP? An interface is an important concept in an object oriented programming.
In this tutorial, we’ll discuss following points.
1. What is an interface?
2. How to create an interface in PHP.
3. What’s the importance of using an interface in our PHP application?
What is an Interface in PHP?
An Interface defines a contract for what a class can do, without saying anything about how the class can do it. Through interfaces, we define what a class can do, the classes that implement interface define how it can do it.
To understand the concept of an interface let’s take an example of a caching system. There are different types of caching available such as File system, Memcache, Redis etc. First, we need to define what a cache can do (I mean different operations), how we do it? we can do it through an interface. A class (Such as Memcache, Redis etc) which implements cache interface will define how they will do it.
NOTE – An interface will not add any additional functionality in your code but it outlines a standard format to which your classes need to use.
Learn PHP programming online – Video tutorials
How to Create & Implement an Interface in PHP
i) An Interface can be created through interface keyword followed by an interface name.
ii) In an interface, methods are not defined, the class which implements must define all the methods of an interface. In short, All the methods in an interface is abstract.
iii) All the methods declare in an interface should be public. A class can implement multiple interfaces whereas a class can extend only one abstract class.
Interface Vs Abstract Class in PHP
iv) In an interface, we can define constants. It works exactly like class constants except these constants cannot be overridden by a class/interface that inherits them.
Let’s create a cache interface which has three methods such as setKey, getKey and deleteKey. Any class which implements Cache interface, define these methods.
1 2 3 4 5 |
interface Cache { public function setKey($key,$value); public function getKey($key); public function deleteKey($key); } |
To use an interface in a class, implements keyword is used.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class FileSystem implements Cache { public $cacheData = array(); public function setKey($key, $value){ $this->cacheData[$key] = $value; } public function getKey($key){ return $this->cacheData[$key]; } public function deleteKey($key) { unset($this->cacheData($key); } } //Memcache have their own way to implement these cache operations class Memcache implements Cache { public function setKey($key, $value){ //method body } public function getKey($key){ //method body } public function deleteKey($key) { //method body } } //Similarly, Redis cache have their own way to implement these operations class Redis implements Cache { public function setKey($key, $value){ //method body } public function getKey($key){ //method body } public function deleteKey($key) { //method body } } |
NOTE – All the methods of an interface must be defined by a class which implements them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
interface Cache { public function setKey($key,$value); public function getKey($key); } /* In a productCache class, we are not implementing getKey($key) method, In that case a fatal error will occur */ class productCache implements Cache { public $productData = array(); public function setKey($key, $value){ $this->productData[$key] = $value; } /* Fatal error: Class productCache contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Cache::getKey) in productCache.php on line 19 */ |
Interface can extend other Interfaces
An interface can extend other interfaces. It can be extended like classes using extends operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
interface A { public function A1(); } //B extends interface A interface B extends A { public function B1(); } // Class implementing interface B class Do implements B { public function A1() { //method body } public function B1() { //method body } } |
A class can implement multiple interfaces
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
interface A { public function A1(); } interface B { public function B1(); } // A Class can implement multiple interfaces class Do implements A, B { public function A1() { //method body } public function B1() { //method body } } |
Some predefined interfaces in PHP.
Why We Use Interfaces in our Code?
i) An interface allows us to hide an implementation detail.
ii) An interface is used to implement multiple behaviors into a single class (build complex types)
In next tutorials, I’ll discuss Dependency injection and interface type hinting in our code.
Conclusion
The use of Interfaces, Abstract Classes, Traits in our Application is not mandatory but it allows you to manage code and keep it well structured.