Introduction to the Interfaces in PHP

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

    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.

    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.

    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];


    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


    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

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Checkout how to install xDebug with pecl on your MacOSX

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.