Introduction to PHP - Understanding Interface Part 2

Guest Posts Paolo Nikko Nuñal

To continue to what we have left regarding interface, let us first examine the code.

<?php
   interface Automobile{
      public function buildChasis();
      public function buildBodyCover();
      public function setEngine($engine);
   }
   class Dodge implements Automobile{
    public function buildChasis(){}
    public function buildBodyCover(){}
    public function setEngine($engine){}
   }
?>


The first one is the Automobile Interface.

 interface Automobile{
//FUNCTIONS GO HERE }


In this example, we have created an interface Automobile. The reason why I used this example is the fact that there are so many ways to create an Automobile and you can have a lot of variants but, the process still remains the same and that is you need an engine for it to run, a chasis and body cover. However, you do have different implementations depending on manufacturer.

And in our example we have created the class Dodge that implements the Automobile interface and will regard it as a basis or blueprint in creating a car. That is why you can see that the Dodge class manages to use all of the functions associated with the Automobile interface.

 class Dodge implements Automobile{
    public function buildChasis(){}
    public function buildBodyCover(){}
    public function setEngine($engine){}
   }


Always remember that the class implementing the interface must use the the same signature in its method, otherwise, the class won't work.