PHP Interface
Just like an abstract class, interface is also created to define the blueprint for classes which inherit the interface. An interface doesn't have abstract methods but public
methods with no definition, and the classes inheriting the interface must provide definition to the methods declare inside the interface.
Syntax for defining PHP Interface
Like classes are defined using the keyword class
, interfaces are defined using the keyword interface
followed by the interface name.
<?php
// interface declaration
interface NameOfInterface {
}
?>
And a class uses the implements
keyword to inherit from an interface and implement the methods declared in the interface.
<?php
// class declaration
class SomeClass implements NameOfInterface {
}
?>
Time for an Example
Let's take a simple example where we will create an interface with some methods declared in it and the class which will implement it will be bound to provide definition 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();
}
?>
We have defined an interface with name WebApp
which has three abstract methods declared in it, namely login()
, register()
and logout()
. As you can see in the code above, we have also provided the parameters that the methods will accept.
Now let's create a class which will implement the above interface:
<?php
// class declaration
class Studytonight 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 we have implemented the interface WebApp
and provided definition for all the methods declared in the interface.
Implementing Multiple Interface
A class can also implement more than one interface. In that case the class will have to provide definition for the methods declared in all the interfaces implemented by the class.
Let's create another interface:
<?php
// interface declaration
interface CMS {
// methods declaration
public function publishPost($post);
}
?>
Now let's add the above interface too, to our class Studytonight
:
<?php
// class declaration
class Studytonight 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 our class Studytonight
implements two interfaces.
Some important points to notice:
- All the methods declared in the interfaces are
public
and do not start with the abstract
keyword.
- If we miss to implement even a single method declared in the interface, in the class implementing the interface, we will get an error.
- Interfaces do not have variables.
Don't worry if its too much for you to understand. We will cover all the points step by step with examples, lets start by understanding how we create an abstract class.
Difference between abstract
Class and Interface
Following are a few important differences between an abstract class and an interface:
Interface | Abstract Class |
An interface cannot have concrete methods in it i.e. methods with definition. | An abstract class can have both abstract methods and concrete methods in it. |
All methods declared in interface should be public | An abstract class can have public , private and protected etc methods. |
Multiple interfaces can be implemented by one class. | One class can extend only one abstract class. |