Understanding Interface in PHP

446
understanding interface and its practical implementation in php

An interface and class are similar but interface cannot contain code inside it. An interface can define method names and arguments, but without the contents inside the methods. Any classes implementing an interface must implement all methods defined by the interface. All methods declared in an interface must be public; this is simply the nature of an interface.

Interfaces are my absolute favorite feature of Object-Oriented Languages. The use of interface becomes very helpful because it helps to organize your code. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.

an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation. Interface can extend or a class can implement multiple other interfaces.

Interface syntax for Implementation

We can declare an interface with interface keyword. Any class that inherits the interface must use implements keyword. A class can inherit multiple interfaces by separating each with comma(,).

interface FirstInterface {}
interface SecondInterface {}

class MyClass implements FirstInterface {
    // defines the interface methods and may have its own code
}

//implementating multiple interfaces
class MySecondClass implements FirstInterface, SecondInterface {
    // defines the interface methods and may have its own code
}

Practical Implementation of Interface

Let’s do some practical implementation of interface on our code. Suppose we have an interface Bird.

interface Bird {
    public function canFly();
    public function canSwim();
}

This is a simple interface with its method signature canFly, and canSwim. Remember that interface doesn’t contain body but only name, visibility and parameters. Visibility should always be public.

Now, when we implement the interface we uses as below:

class Duck implements Bird
{
    public function canFly() {
        echo 'Duck can fly';
    }

    public function canSwim() {
        echo 'Duck can swim';
    }
}

If you implement a interface, you must satisfy all its methods. For example, we use canFly and canSwim method in Duck class. Otherwise, it will display an error message.

Fatal error: Class Classname contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

An Interface is provided so you can describe a set of functions and then hide the final implementation of those functions in an implementing class. This allows you to change the implementation of those functions without changing how you use it.

For example I have a database. I want to write a class that accesses the data in my database. I define an interface like this:

interface Database {
    function listOrders();
    function addOrder();
    function removeOrder();
}

Then let’s say, we started using MYSQL database. Then, we can write a class to access MYSQL database:

class MySqlDatabase implements Database {
    public function listOrders() {
        //mysql list order code
    }
    public function addOrder() {
        //mysql add order code
    }
    public function removeOrder() {
        //mysql remove order code
    }
}

we write these methods as needed to get to the MySQL database tables. Then you can write your controller to use the interface as such:

$database = new MySqlDatabase();
foreach ($database->listOrders() as $order) {
    //your code
}

Let us define another class for accessing MongoDB database instead of MYSQL as like:

class MongoDatabase implements Database {
    public function listOrders() {
        //mongoDB list order code
    }
    public function addOrder() {
        //mongoDB add order code
    }
    public function removeOrder() {
        //mongoDB remove order code
    }
}

Then, we decided to switch from MYSQL to MongoDB. To switch our application to use the MongoDB database instead of the MySQL database we only have to change ONE LINE of code:

$database = new MongoDatabase();

all other lines of code, such as:

foreach ($database->listOrders() as $order) {

will remain unchanged. The point is – the Interface describes the methods that we need to access our database. It does NOT describe in any way HOW we achieve that. That’s what the implementing class does. We can implement this interface as many times as we need in as many different ways as we need. We can then switch between implementations of the interface without impact to our code because the interface defines how we will use it regardless of how it actually works.

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.