Interface in PHP

  • Avinash
  • 7
  • Oct 31, 2011
  • Tutorial

Brief About Interface

Most of the design patterns use interfaces, but interfaces are no design pattern. So from the programming point of view it will not add any additional functionality to your code but it will help up to make some standards for the code you have written.

Interface is some what like as abstract class, you can find more about the abstract class from this article on Abstract in PHP.

Interface is just like a coding standard because interface allow us to create a code/class which specifies the list of methods a class must implement.

What can be declared in Interface?

We can declare methods and constants in interface. Make sure that declared method must have only declaration part and not the implementation part. Also all methods declared in interface must be public.

Note: All methods declared in interface must be public!!

How to Create Interface?

We can create an interface using the keyword interface. Check out below code that will demonstrate the code to create an basic interface.

interface first_interface { public function first_method($name); public function second_method(); }
  interface first_interface
  {
    public function first_method($name);
    public function second_method();
  }

You can see that above interface have 2 methods declared. So any class which implement this interface must declare those two methods.

Implementing the Interface

Interface can be implemented using implements operator. All methods declared in the interface must be implemented in class which implement that interface.

It will generate Fatal Error if you do not implement the method which declared in an interface. Please check below code which shows the implementation of the interface.

interface first_interface { public function first_method($name); public function second_method(); } class new_class implements first_interface { public function first_method($name) { // Code goes Here for method } public function second_method() { // Code goes Here for method } }
  interface first_interface
  {
    public function first_method($name);
    public function second_method();
  }

 class new_class implements first_interface
 {
    public function first_method($name)
    {
      // Code goes Here for method
    }

    public function second_method()
    {
      // Code goes Here  for method
    }
 }

We can implement more than one interface to one class. In this case interface names should be separated by comma ,.

class new_class implements first_interface, second_interface { // Code for the class goes here. }
  class new_class implements first_interface, second_interface
  {
     // Code for the class goes here.
  }

Extending the Interface

Same as classes we can extend the interface too using the extends keyword. So once any class implements the interface must implemets all methods from the both parent and child interfaces. Have a look at below code block for the same:

interface first_interface { public function first_method($name); } interface second_interface extends first_interface { public function second_method(); } // This class is implementing the second interface. // And second_interface is exteding the first_interface. // So this class needs to implement // all methods declared in both interfaces. class new_class implements second_interface { public function first_method($name) { // Code goes Here method } public function second_method() { // Code goes Here for method } }
   interface first_interface
   {
     public function first_method($name);
   }

  interface second_interface extends first_interface
  {
     public function second_method();    
  }

  // This class is implementing the second interface.
  // And second_interface is exteding the first_interface.
  // So this class needs to implement
  // all methods declared in both interfaces.
  class new_class implements second_interface
  {
      public function first_method($name)
      {
         // Code goes Here  method
       }
   
      public function second_method()
      {
        // Code goes Here  for method
      }
  }

Note: We can not implements two interfaces if same method declared in both interfaces.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Bhumi

    Interface support multiple inheritance in php?

    • http://www.xpertdeveloper.com Avinash

      PHP does not support multiple inheritance and interface is a part of the PHP…

      • Alex

        That’s actually not true: a class can implement multiple interfaces in PHP. Further more, an interface can extend multiple other interfaces. That’s actually one of the reasons interfaces exist: to provide a compromise in the way of multiple inheritance.

  • ludalito

    IMHO, you miss one important thing about interfaces: they describe behaviours.
    Plus, most of the design patterns use interfaces, but interfaces are no design pattern.

  • Pingback: PHP const Keyword Explained | Expert PHP Developer

  • http://www.dumagueterview.com Andy Gee

    Hi,
    I’ve always used functions as opposed to classes and interfaces. Are there any advantages to using classes or interfaces as opposed to functions?

  • Pingback: Predefined Interfaces in PHP | Expert PHP Developer