PHP interfaces

This tutorial teaches you about PHP Interfaces. 

 

What is PHP Interface?

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.

 

Syntax for defining PHP 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.

 

Implementing Multiple 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:

  • Methods declared in the interfaces are public. The methods do not start with the abstract keyword.
  • On missing to implement even a single method declared in the interface, we will get an error in the class implementing the interface.
  • Interfaces do not have variables.

All the points will be covered step by step with examples. Let's start with how to create an abstract class.

 

Difference between abstract Class and Interface

Following are a few important differences between an abstract class and an interface:

InterfaceAbstract 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.