The interface is another property of PHP that enables the developer to build a program without any complex methods. It inherits the same public method that a class holds. Due to these properties, an interface can define methods. Still, we cannot define the method in that particular interface, and we have to define the method inside the child class where we call the interface. Interfaces are also similar to classes, and the only difference is class is altered with an interface, and the methods do not have any content defined inside. We use interfaces in PHP.
A single class cannot implement two functions with the same method name as the compiler will return an error. Similarly, like traits, we can also use an interface to access multiple inheritances as a class can initiate multiple interfaces, which will eventually help in inheriting traits from multiple parent classes
Example
Output:
In the above program, we have declared an interface fruit with a function taste[] inside. We have declared a class mango and assigned the interface fruit to it; we have defined the function taste[]. Finally, we have created an object all called our child class. Due to the interface, our child class mango can access the interface fruit and retrieve the function taste[] from it
Uses of Interface
1] To inherit properties of the parent class
To implement an interface using class, we have to use the implement keyword and the expand keyword to assign the hierarchy of the parent class
Syntax:
Example:
Output:
this is the first class this is interface which is defined inside child class this is an inherited child class using an interface
In the above program, we have a parent class parentClass which contains a function parentFunction[], and we have an interface called firstInterface, which holds the function named interfaceFunction[]. Still, we cannot define the declared function inside our created interface, and we have to define the declared function inside the child class. In the end, we have a child class with the name childClass with function childFunction[] and inherited the function from the interface using the extends and implement property of PHP. Also, we have defined our interfaceFunction[] in the child. And to access the classes and interface, we have created an object obj and invoked all the functions using the same object.
2] To inherit properties of multiple interfaces
To implement an interface using class, we have to use the implement keyword, and a single class can even implement more than one interface simultaneously
Example:
Output:
this is the first interface this is the second interface this is a child inherited from both interfaces
In the above program, we have declared two interfaces, first with the name firstInterface with function interfaceone[] and second with name secondInterface with function onterfacetwo[]. Still, we have only declared the interface functions and will define the same function in the child class. In the end, we have a child class with the name childClass with function childFunction[] and inherited both the interfaces using the implements clause. Inside the function, we have given the definition of functions declared in interfaces. And to access the classes and interfaces, we have created an object with the name obj and called all the functions using the same object.
Importance of using Interfaces:
- The interface provides the developer with a more structured format that is quite difficult to achieve with classes.
- We can easily use the multiple inheritance property of OPPs in a single strand programming language like PHP using an interface.
- By implementing an interface, the object's caller needs to care only about the object's interface, not implementations of the object's methods.
- The interface allows unrelated classes to implement the same methods regardless of their positions in the class inheritance hierarchy.
- We cannot implement multiple interfaces with the same method name as it will end up in confusion and method ambiguity.
- We can easily make a hierarchy between two classes using the extend keyword.
Why would you need an interface, if there are already abstract classes? To prevent multiple inheritance [can cause multiple known problems].
One of such problems:
The "diamond problem" [sometimes referred to as the "deadly diamond of death"] is an ambiguity that arises when two classes B and C inherit from A and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
Source: //en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem
Why/When to use an interface? An example... All cars in the world have the same interface [methods]... AccelerationPedalIsOnTheRight[]
, BrakePedalISOnTheLeft[]
. Imagine that each car brand would have these "methods" different
from another brand. BMW would have The brakes on the right side, and Honda would have brakes on the left side of the wheel. People would have to learn how these "methods" work every time they would buy a different brand of car. That's why it's a good idea to have the same interface in multiple "places."
What does an interface do for you [why would someone even use one]? An interface prevents you from making "mistakes" [it assures you that all classes which implement a specific interface, will all have the methods which are in the interface].
// Methods inside this interface must be implemented in all classes which implement this interface.
interface IPersonService
{
public function Create[$personObject];
}
class MySqlPerson implements IPersonService
{
public function Create[$personObject]
{
// Create a new person in MySql database.
}
}
class MongoPerson implements IPersonService
{
public function Create[$personObject]
{
// Mongo database creates a new person differently then MySQL does. But the code outside of this method doesn't care how a person will be added to the database, all it has to know is that the method Create[] has 1 parameter [the person object].
}
}
This way, the Create[]
method will always be used the same way. It doesn't matter if we are using the MySqlPerson
class or the MongoPerson
class. The way how we are using a method stays the same [the interface stays the same].
For example, it will be used like this [everywhere in our code]:
new MySqlPerson[]->Create[$personObject];
new MongoPerson[]->Create[$personObject];
This way, something like this can't happen:
new MySqlPerson[]->Create[$personObject]
new MongoPerson[]->Create[$personsName, $personsAge];
It's much easier to remember one interface and use the same one everywhere, than multiple different ones.
This way, the inside of the Create[]
method can be different for different classes, without affecting the "outside" code, which calls this method. All the outside code has to know is that the method Create[]
has 1 parameter [$personObject
], because that's how the outside code will use/call the method. The outside code doesn't care what's happening inside the method; it only has to know how to use/call it.
You can do this without an interface as
well, but if you use an interface, it's "safer" [because it prevents you to make mistakes]. The interface assures you that the method Create[]
will have the same signature [same types and a same number of parameters] in all classes that implement the interface. This way you can be sure that ANY class which implements the IPersonService
interface, will have the method Create[]
[in this example] and will need only 1 parameter [$personObject
] to get called/used.
A class that implements an interface must implement all methods, which the interface does/has.
I hope that I didn't repeat myself too much.