Home » PHP » PHP Interfaces
This tutorial teaches you about PHP Interfaces.
An interface is created for defining the blueprint for classes that inherit the interface. It is similar to abstract classes. However, an interface doesn't have abstract methods but public methods with no definition. Therefore, the classes inheriting the interface must provide a definition to the methods declared inside the interface.
You can define the interfaces using the keyword interface followed by the interface name.
<?php
// interface declaration
interface NameOfInterface {
}
?>
For inheriting from an interface and implementing the methods declared in the interface, the class uses the implements keyword.
<?php
// class declaration
class SomeClass implements NameOfInterface {
}
?>
Let's take an example. In it, an interface will be created with some methods declared in it, and the class that will implement it will be bound to provide definitions for those methods.
Following is our interface:
<?php
// interface declaration
interface WebApp {
// methods declaration
public function login($email, $password);
public function register($email, $password, $username);
public function logout();
}
?>
An interface has been defined with the name WebApp. This interface has three abstract methods declared in it, namely login(), register(), and logout(). The parameters that the methods will accept in the code above have also been provided.
Let us create a class that will implement the interface mentioned above:
<?php
// class declaration
class Developer implements WebApp {
// methods definition
public function login($email, $password) {
echo "Login the user with email: " . $email;
}
public function register($email, $password, $username) {
echo "User registered: Email=".$email." and Username=".$username;
}
public function logout() {
echo "User logged out!";
}
}
?>
In the class above, the interface WebApp has been implemented, and we have provided a definition for all the methods that have been declared in the interface.
More than one interface can also be implemented by a class. In such cases, the class will have to provide definitions for the methods declared in all the interfaces that a class implements.
We will create another interface:
<?php
// interface declaration
interface CMS {
// methods declaration
public function publishPost($post);
}
?>
Now we will add the above interface too, to our class Developer:
<?php
// class declaration
class Developer implements WebApp, CMS {
// methods definition
public function login($email, $password) {
echo "Login the user with email: " . $email;
}
public function register($email, $password, $username) {
echo "User registered: Email=".$email." and Username=".$username;
}
public function logout() {
echo "User logged out!";
}
public function publishPost($post) {
echo $post." published!";
}
}
?>
Now two interfaces will be implemented by our class Developer.
Points to remember:
All the points will be covered step by step with examples. Let's start with how to create an abstract class.
Following are a few important differences between an abstract class and an interface:
Interface | Abstract Class |
---|---|
An interface cannot have concrete methods, that is, methods with definition. | An abstract class can contain both concrete methods and abstract methods in it. |
Methods declared in the interface should be public. | The methods an abstract class can have are public, private, protected, etc. |
One class can implement multiple interfaces. | One class can extend only one abstract class. |