Tutorial

Interface in PHP

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 [code]interface[/code]. Check out below code that will demonstrate the code to create an basic interface.

[cc lang=”php”]
interface first_interface
{
public function first_method($name);
public function second_method();
}
[/cc]

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 [code]implements[/code] 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.

[cc lang=”php”]
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
}
}

[/cc]

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

[cc lang=”php”]
class new_class implements first_interface, second_interface
{
// Code for the class goes here.
}
[/cc]

Extending the Interface

Same as classes we can extend the interface too using the [code]extends[/code] 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:

[cc lang=”php”]
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
}
}
[/cc]

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

Shares:
  • Bhumi
    Bhumi
    October 31, 2011 at 12:19 am

    Interface support multiple inheritance in php?

    Reply
    • Avinash
      October 31, 2011 at 12:21 am

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

      Reply
      • Alex
        Alex
        October 31, 2011 at 4:43 pm

        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.

        Reply
  • ludalito
    ludalito
    October 31, 2011 at 3:28 pm

    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.

    Reply
  • […] in interface also. If you are not sure what is Interface then you can refer this article for Interface in PHP.If we are inside the class then values of the constants can be get using self keyword, but […]

    Reply
  • Andy Gee
    February 13, 2012 at 4:29 am

    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?

    Reply
  • […] So same as these constants we have set of Interface also which are already declared by the PHP. If you are not sure what is Interface in PHP, then I would suggest to read this article related to Interface In PHP. […]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *