Author: Dave Smith
Viewers: 375
Last month viewers: 17
Categories: PHP Tutorials
Read this tutorial to learn the different ways to implement interfaces and set up interface dependencies in PHP.
Introduction
Short Review of Abstracts
Basic Interface
Implementing Multiple Interfaces
Interface Dependency
Constants and Interfaces
Conclusion
Introduction
Please note that examples in this tutorial are not complete models and are provided to illustrate a concept.
Short Review of Abstracts
Before we get started with Interfaces, a short review of Abstracts will help us understand these concepts. An Abstract Class can provide abstract and common methods.
The abstract methods provide the method signature (function name and parameters) which a concrete class extending it must use when implementing. Understanding this part of abstraction and you are well on your way to understanding Interfaces.
The common methods which are fully implemented in an Abstract Class can be used by a concrete class through inheritance. This is the basic principle of Traits, which is a newer PHP language feature which will be discussed in another article.
Basic Interface
In PHP, interfaces are really nothing more than a template of method signatures for other interfaces or concrete classes to implement. An interface is implemented using the implements operator. How simple is that?
interface oopTemplate { public function methodOne( $userName ); public function methodTwo( $userName, $password ); } class oopClass implements oopTemplate { public function methodOne( $userName ) { echo 'The user name is '. $userName. '<br>'; } public function methodTwo( $userName, $password ) { echo 'The user name is '. $userName. '<br>'; echo 'The password is '. $password. '<br>'; } }
In the above example we have an interface named oopTemplate which contains two method signatures and a class named oopClass with implements the interface by fully implementing the required methods. It is important to note that all method signatures in an interface are public.
Implementing Multiple Interfaces
We can also implement more than one interface by separating them with a comma after the implements operator.
interface oopTemplate { public function methodOne( $userName ); public function methodTwo( $userName, $password ); } interface anotherTemplate { public function methodOne( $userName ); public function methodThree( $userName, $password ); } class oopClass implements oopTemplate, anotherTemplate { public function methodOne( $userName ) { echo 'The user name is '.$userName.'<br>'; } public function methodTwo( $userName, $password ) { echo 'The user name is '.$userName.'<br>'; echo 'The password is '.$password.'<br>'; } public function methodThree( $userName, $password ){ if( $userName == $password ) { return true; } else { return false; } } }
As we can see, the class now implements both the oopTemplate and anotherTemplate and has fully implemented the required methods in both interfaces.
Notice that the anotherTemplate interface also has a method signature named methodOne, just like the oopTemplate interface.
In newer versions of PHP, since version 5.3.9, this is allowed as long as the method signatures are the same. If the method signatures are not the same, or in previous PHP versions, the duplicate method signatures create ambiguity and the script will fail with a fatal error.
Interface Dependency
An interface can also extend another interface which allows us to define dependencies.
interface oopTemplate { public function methodOne( $userName ); public function methodTwo( $userName, $password ); } interface anotherTemplate extends oopTemplate { public function methodOne( $userName ); public function methodThree( $userName, $password ); } class oopClass implements anotherTemplate { public function methodOne( $userName ) { echo 'The user name is '.$userName.'<br>'; } public function methodTwo( $userName, $password ) { echo 'The user name is '. $userName. '<br>'; echo 'The password is '. $password. '<br>'; } public function methodThree( $userName, $password ){ if( $userName == $password ){ return true; }else{ return false; } } }
The above example is nearly identical to the previous one, however we have now used the anotherTemplate interface to be dependent on the oopTemplate interface by extending it. The oopClass now only implements the anotherTemplate interface yet still must fully implement all the methods as before.
Constants and Interfaces
While we are not able to define class properties in interfaces, we can define constants.
interface oopTemplate { const memberConstant = 'password'; public function methodOne( $userName ); public function methodTwo( $userName, $password ); } interface anotherTemplate extends oopTemplate { public function methodOne( $userName ); public function methodThree( $userName, $password ); } class oopClass implements anotherTemplate { public function methodOne( $userName ) { echo 'The user name is '. $userName. '<br>'; } public function methodTwo( $userName, $password) { echo 'The user name is '. $userName. '<br>'; echo 'The password is '. $password. '<br>'; } public function methodThree( $userName, $password ) { if( $password == self::memberConstant ) { return true; } if( $userName == $password ){ return true; } else { return false; } } }
The oopTemplate interface defines a constant named memberConstant with a value of 'password'. The oopClass now uses this constant to perform a logical check to determine if the $password property is the same as the constants value.
An interface constant can only be defined once so other interfaces or classes that inherit them are not allowed to change or override them.
Conclusion
Most development projects are closely related and use abstract classes to provide partial implementations along with method signatures.
If all you want to provide is a template of method signatures, with or without dependency, to a broad range of classes then the Object Interfaces are the way to go.
If you liked this article or have questions about PHP object interfaces, post a comment here.
You need to be a registered user or login to post a comment
1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
3. Abstract Class vs. Interface - D (2015-07-23 02:27)
Abstract Class vs. Interface: when to use which?... - 1 reply
Read the whole comment and replies
2. simple and good exaplanation - C. C. Ng (2015-07-22 16:15)
simple and good expanation on the PHP interface... - 0 replies
Read the whole comment and replies
1. Thanks for that - Yoni Lamri (2015-07-22 16:14)
interface in OOP... - 0 replies
Read the whole comment and replies