Introduction to the Interfaces in PHP

Last Updated at : October 4, 2017 By - Usama Muneer | 1458

    Interfaces are used in modern development a lot. In this short article, we’ll take a look on how to utilise interfaces better with PHP by creating an Interface and building up some classes to use it.

    What is an Interface?

    An Interface maybe defined as a group of methods required to build a structure within an application.

    Basic Example

    For creating a new interface, you need to use the Interface keyword following by the interface_name.

    <?php
    interface car {
       public function color();
       public function engine();
       public  function made();
    }

    Here we’ve a car interface, as you may know we’re surrounded by a variety of cars. But all cars have some variable attributes like color, engine or year made. Let’s create a new BMW class and see how it used.

    <?php
    class BMW implements car {
       public function color() {
          // TODO: Implement color() method.
       }
    
       public function engine(  ) {
          // TODO: Implement engine() method.
       }
    
       public  function made() {
          // TODO: Implement made() method.
       }
    }

    More Practical Example

    Let’s assume, we need to store something somewhere, so we’ll be building a class to store a particular value under a particular key & also will retrieve the value from either session, cookie or Database whatsoever.

    Creating an Interface

    Well let’s create the interface and name it StorageInterface.php

    interface StorageInterface {
       public function set($key, $value);
       public function get($key);
    }

    Creating a Class

    Let’s now create a SessionStorage class which will be using the methods defined in our StorageInterface. Well to use an interface, we’ll use the implements keyword.

    class SessionStorage implements StorageInterface {
       public function set( $key, $value ) {
          $_SESSION[$key] = $value;
       }
    
       public function get( $key ) {
          return $_SESSION[$key];
       }
    }

    Note

    One thing to take care of while developing your application is whenever you implements something from an Interface, you are bound to use every single class inside of that Interface. You can’t just bypass any method while implementing an Interface. You can use a Trait for that reason maybe. But that’s another topic.

    Instantiating the class

    Now in our index.php or whatever your default root file, we’ll instantiate the SessionStorage class to see how it works.

    $session = new SessionStorage();

    Setting the Session

    $session->set('Name','TheWebTier');

    Getting the Session

    echo $session->get('Name');

    Final Words

    Interfaces are considered an important element while developing larger applications. Interfaces also helps DRYing up your code-base by restricting you to re-write code for every single class.

    You can create other DatabaseStorage, CookieStorage classes to separate logic for each. Try it yourself and let us know if you face any issues in between. Leave us your comments or feedback in the comment box. You can also follow us on Twitter.

    Related Readings

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Inspired from Laravel's router, lets build our very own PHP's router for managing our endpoints well for our next PHP project.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.

    There are several PHP frameworks for a web development project. Among them Laravel is regarded as the best open-source PHP web framework.