PHP Interfaces Tutorial

In this tutorial we learn about another form of composition in PHP, interfaces. Interfaces are similar to abstract classes and allow us to build loosely coupled applications.

What is an interface?

Interfaces are similar to abstract classes in the sense that their members are not implemented.

If you remember our tutorial on composition , we spoke about favoring a Has-A relationship over an Is-A relationship. To quickly recap:

  • Inheritance can be abused, which may lead to a large, fragile, hierarchy of classes.
  • Inheritance makes our classes tightly coupled and dependent on each other. We want them to be as loosely coupled as possible.

An interface allows us to have Has-A relationships between classes. Instead of defining a class for each relationship, we define interfaces.

While interfaces have nothing to do with inheritance, they do allow for polymorphic behavior in a similar way.

How to define an interface

An interface is defined the same way as a class, except for the interface keyword. Interface methods are defined the same way as abstract methods except for the abstract keyword.

Syntax:
interface IInterfaceName {

    function methodName();

}

When defining an interface, we should consider:

  • The interface name is prefixed with the capital letter I, which is the normal convention for naming interfaces.
  • Interface methods do not specify their access, they are automatically public.
  • Similar to abstract methods, an interface method does not have an implementation and the statement is terminated with a semicolon.
Example: define an interface
<?php

interface IFlyable {

    function fly($speed);

}

?>

In the example above, we create an interface with one method. The method will have to be overridden in a child class.

How to implement an interface

Interfaces are implemented, rather than inherited. We implement an interface with the implements keyword.

Syntax:
interface IInterfaceName {

}

class ClassName implements IInterfaceName {

}
Example: implement an interface
<?php

// Interface
interface IFlyable {

    function fly($speed);

}

// Implementations
class Sparrow implements IFlyable {

    function fly($speed) {
        echo "Sparrow flying at a speed of " . $speed . "<br>";
    }

}

class Plane implements IFlyable {

    function fly($speed) {
        echo "Plane flying at a speed of " . $speed . "<br>";
    }

}

// Usage
$sparrow1 = new Sparrow();
$sparrow1->fly(10);

$plane1 = new Plane();
$plane1->fly(100);

?>

In the example above, we have a Sparrow and Plane class that implement the IFlyable interface. Both classes have to implement their own version of the fly() method.

How to implement multiple interfaces

One of the benefits of interfaces over abstract classes is that we can implement more than just one.

Interfaces aren’t used for multiple inheritance, it’s just a benefit we get when using them.

To implement multiple interfaces, we simply separate them with a comma.

Syntax:
interface IInterfaceName1 {

}

interface IInterfaceName2 {

}

class ClassName implements IInterfaceName1, IInterfaceName2 {

}
Example: implement multiple interfaces
<?php

// Interfaces
interface IFlyable {
    function fly($speed);
}

interface IHoppable {
    function hop($speed);
}

// Implementations
class Sparrow implements IFlyable, IHoppable {

    function fly($speed) {
        echo "Sparrow flying at a speed of " . $speed . "<br>";
    }
    function hop($speed) {
        echo "Sparrow hopping at " . $speed . " hops per minute<br>";
    }

}

class Plane implements IFlyable {

    function fly($speed) {
        echo "Plane flying at a speed of " . $speed . "<br>";
    }

}

// Usage
$sparrow1 = new Sparrow();
$sparrow1->fly(10);
$sparrow1->hop(30);

$plane1 = new Plane();
$plane1->fly(100);

?>

In the example above, we create another interface called IHoppable. Because a sparrow can both fly and hop around, it can implement both interfaces.

Everything is loosely coupled, so if we wanted to add a Bunny class, all we need to do is implement the IHoppable interface.

Summary: Points to remember

  • Interfaces allow for a loosely coupled Has-A relationship between classes.
  • Similar to abstract classes, interfaces force a developer to provide their own implementation.
  • Classes can implement multiple interfaces.