Language:

Search

Introduction to the Interfaces in PHP

  • Share this:
Introduction to the Interfaces in PHP

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

Usama Muneer

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.