Introduction to the Interfaces in PHP

October 1, 2017 By - Usama Muneer | 703

    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

    When it comes to validation in Laravel. Developers often refer to using separate Requests to achieve it. Now for creating and updating, rules might get a little bit tricky. But we've got you covered.

    While Developing applications in Laravel, there comes situations where you have date in simple string format. For instance getting raw response from an API. But you want to convert that string into Carbon instance and use the Carbon helpers on it. Well there's a simple solution to it.

    If you have encountered an error wp.media not defined while uploading images for your WordPress custom posts or taxonomies.

    Laravel ships in with the awesomeness of Database Migrations which allows you to version control your database. But there are number of events when you are required to change the datatypes or attributes assigned to your table columns inside of your migrations.