Introduction to PHP Interface
PHP Interface helps us to make useful programs, indicates the public methods of the class that must execute without even including the complexities and how our specific methods are to be implemented. PHP Interface will only define arguments and names rather than the contents of the methods and any class which implements the interface need to implement all the methods which are defined by the interface.
These interfaces are very much similar to the class but only class phrase will be replaced by the interface keyword in the declaration.
Syntax:
<?php
Interface interface_name
{
//List of methods
}
?>
Why Do We Need PHP Interface?
It can implement one interface in the same class but it also has the capability of making more interfaces within the same class. PHP Object interfaces allow us to create the code which methods a class must need to implement without defining how the methods are implementing. Interface is the same or similar way of the class but the interface keyword of PHP replaces the class keyword without using any methods which are having their contents defined. A constructor can also be declared in an interface. Class or Classes can implement multiple interfaces when need by separating each and every interface with the comma.
It can also be extended just like the classes using the “extends” operation in PHP Programming Language. PHP Interfaces are the next level of abstraction. It is similar to the abstract classes but there is a slight difference. Interface or Interfaces allow you to create the code which is helpful to define the methods of the classes but you can’t even add any type of code to those methods whereas the abstract classes allow the same or similar thing as the PHP interface or interfaces but in abstract classes, one can add the code to the method or methods.
Working of PHP Interface
Older versions before PHP 5.3.9 version couldn’t even implement just two interfaces that have the same name because it will cause some ambiguity. Most of all the recent PHP versions allow duplicate methods which is having the same/similar signature. The class which implements the interface should use the method signatures which are compatible with the “Liskov Substitution Principle – LSP”. There will be a fatal error in the result if the method signatures are not used.
The interface will be made only with methods with no content placing in it but a class is made with the interface to add the content to the interface methods. Methods that are in the interfaces are visible to the public with no restrictions mostly. PHP interfaces are not the same/similar to the classes. Classes can inherit many/multiple interfaces but class can inherit only one class at a time. It has this advantage too. Variables are present inside of the interface/interfaces.
Examples of PHP Interface
Below are the examples of PHP Interface:
Example #1
In the below example, we declared an interface with the name “MyInterfaceName1” with only two methods mentioned in it. They are method11 and method21 inside of the interface without using any content. Then a class with the name “MyClassName1” will implement the interface “MyInterfaceName1” and then uses the methods which are available based on the requirement.
Just like the definition said, here also the interface only consists of methods without any content. Then content will be mentioned only in the class which will implement the interface. So these interface method/methods are also called abstract methods some times.
Code:
<?php
interface MyInterfaceName1{
public function method11();
public function method21();
}
class MyClassName1 implements MyInterfaceName1{
public function method11(){
echo "Now Method11 Called" . "<br>";
}
public function method21(){
echo "Now Method21 Called". "\n";
}
}
$obj = new MyClassName1;
$obj->method11();
$obj->method21();
?>
Output:
Example #2
In the below program, two interfaces are created with a method in each. Then a class Bird is made with a method called info(). Now the class Dove extends to the Class Bird which implements the CanFly PHP Interface to get the print statements because print statements/other will not be present inside the interfaces because it will have only methods.
Based on the characteristics of the Bird 2 interfaces are made “CanFly” and “CanSwim” with 1 method in each. They are fly() and swim(). Now class “Bird” is made with the method “info()” to get what type of bird is this and whether it is a bird or not using the print/echo statements. Then classes like “Dove”, “Penguin” and “Duck” are one by one by placing the details of the birds in the “$name1” variable by extending the “bird” class and also by implementing the “CanFly” and “CanSwim” interfaces with the echo statements for the interfaces is made.
Then a function is made “describe($bird)” to know whether it is bird or not using the “CanFLy” and “CanSwim” interfaces. If the $bird doesn’t satisfy the fly() and swim() then the else condition comes into existence into the output of the PHP program. Now calling all the describe functions with the 3 bird classes. You will get the output of the 3 birds as needed. Check out below. It is the simple description but you will know how is happening if you look at the code and output of the PHP Program below.
Code:
<?php
/**
* This is An example of the duck typing in the PHP Script
*/
interface CanFly1 {
public function fly1();
}
interface CanSwim1 {
public function swim1();
}
class Bird1 {
public function info1() {
echo "I am a {$this->name1}\n";
echo "I am a bird\n";
}
}
/**
* This is some of the implementations of the birds
*/
class Dove1 extends Bird1 implements CanFly1 {
var $name1 = "Dove";
public function fly1() {
echo "I fly\n";
}
}
class Penguin1 extends Bird1 implements CanSwim1 {
var $name1 = "Penguin";
public function swim1() {
echo "I swim\n";
}
}
class Duck1 extends Bird1 implements CanFly1, CanSwim1 {
var $name1 = "Duck";
public function fly1() {
echo "I fly\n";
}
public function swim1() {
echo "I swim\n";
}
}
/**
* This is one of the simple function which is to describe a bird
*/
function describe1($bird1) {
if ($bird1 instanceof Bird1) {
$bird1->info1();
if ($bird1 instanceof CanFly1) {
$bird1->fly1();
}
if ($bird1 instanceof CanSwim1) {
$bird1->swim1();
}
} else {
die("This is not a bird. I cannot describe it.");
}
}
// Now describing the birds
describe1(new Penguin1);
echo "---\n<br>";
describe1(new Dove1);
echo "---\n<br>";
describe1(new Duck1);
Output:
Advantages
Following are some of the advantages given.
- It will allow the unrelated classes to implement a similar set of methods without taking into the consideration of their positions in the class which has inheritance hierarchy.
- It can able to model many or multiple inheritances because in the interface a class can be used to implement more interfaces but it can only extend one single class.
- It can implement an inheritance which can save the caller from the implementation of the object methods fully. This implementation of the heritance concept is also helpful to focus on the objects interface so that the caller interface doesn’t even affect.
Recommended Article
This is a guide to the PHP Interface. Here we discuss the PHP Interface, Advantages and it’s various examples to understand the PHP Interface concept briefly. You can also go through our other suggested articles to learn more –