An Interface allows the users to create programs, specifying the public methods that a class must implement, without involving the complexities and details of how the particular methods are implemented. It is generally referred to as the next level of abstraction. It resembles the abstract methods, resembling the abstract classes. An Interface is defined just like a class is defined but with the class keyword replaced by the interface keyword and just the function prototypes. The interface contains no data variables. The interface is helpful in a way that it ensures to maintain a sort of metadata for all the methods a programmer wishes to work on.
Creating an Interface
Following is an example of how to define an interface using the
interface keyword.
php
<?php
interface MyInterfaceName {
public function methodA();
public function methodB();
}
?>
Few characteristics of an Interface are:
- An interface consists of methods that have no implementations, which means the interface methods are abstract methods.
- All the methods in interfaces must have public visibility scope.
- Interfaces are different from classes as the class can inherit from one class only whereas the class can implement one or more interfaces.
To implement an interface, use the
implements operator as follows:
php
<?php
class MyClassName implements MyInterfaceName{
public function methodA() {
// method A implementation
}
public function methodB(){
// method B implementation
}
}
?>
Concrete Class: The class which implements an interface is called the Concrete Class. It must implement all the methods defined in an interface. Interfaces of the same name can’t be implemented because of ambiguity error. Just like any class, an interface can be extended using the
extends operator as follows:
php
<?php
interface MyInterfaceName1{
public function methodA();
}
interface MyInterfaceName2 extends MyInterfaceName1{
public function methodB();
}
?>
Example:
php
<?php
interface MyInterfaceName{
public function method1();
public function method2();
}
class MyClassName implements MyInterfaceName{
public function method1(){
echo "Method1 Called" . "\n";
}
public function method2(){
echo "Method2 Called". "\n";
}
}
$obj = new MyClassName;
$obj->method1();
$obj->method2();
?>
Output:
Method1 Called
Method2 Called
Advantages of PHP Interface
- An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
- An interface can model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.
- The implementation of an inheritance will save the caller from full implementation of object methods and focus on just he objects interface, therefore, the caller interface remains unaffected.
Similar Reads
PHP Namespace A namespace in PHP is a container for logically grouping classes, interfaces, functions, and constants. They help avoid name collisions by allowing the same name to be used in different namespaces without conflict. Using namespaces, you can make your code more organized, maintainable, and scalable.A
4 min read
Traits vs. Interfaces in PHP The main difference between the Traits and Interfaces in PHP is that the Traits define the actual implementation of each method within each class, so many classes implement the same interface but having different behavior, while traits are just chunks of code injected in a class in PHP. Traits Trait
3 min read
PHP interface_exists() Function The interface_exists() function is an inbuilt function in PHP that checks interface is defined or not. Syntax: bool interface_exists(string $interface, bool $autoload = true)Parameters: This function accept two parameters that are described below: $interface: This parameter holds the interface name.
1 min read
PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi
8 min read
When do we need Interfaces in PHP? Interface are definitions of the public APIs that classes (implementing an interface) must implement. It is also known as contracts as an interface allows to specify a list of methods that a class must implement. Interface definition is similar to the class definition, just by changing the keyword c
3 min read