Why interfaces are used in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Interface are definitions of the public APIs that classes (implementing an interface) must implement. It is also known as contracts as an interface allows to specify a list of methods that a class must implement. Interface definition is similar to the class definition, just by changing the keyword class to interface.
    Example:
     

    php

    Interfaces can contain methods and/or constants, but no attributes. Interface constants have the same restrictions as class constants. Interface methods are implicitly abstract.
    Example:
     

    php

    interface IMyInterface {

        const INTERFACE_CONST_1 = 1;

        const INTERFACE_CONST_2 = 'a string';

        public function method_1();

        public function method_2();

    }

    ?>

    Any class that needs to implement an interface must do using the implements keyword. A single class can implement more than one interface at a time.
     

    php

    class MyClass implements IMyInterface {

        public function method_1() {

        }

        public function method_2() {

        }

    }

    Interesting Points : 

    • A class cannot implement two interfaces that has the a same method name because it would end up with the method ambiguity.
    • Like classes, it is possible to establish an inheritance relationship between interfaces by using the same keyword “extends”. 
      Example: 
       

    php

    interface Foo {

    }

    interface Bar {

    }

    interface Bass extends Foo, Bar {

    }

    Below is a complete example that shows how interfaces work. 
    Example: 
     

    php

    interface Shape {

        public function getColor();

        public function setColor($color);   

        public function describe();

    }

    class Triangle implements Shape {

        private $color = null;

        public function getColor() {

            return $this->color;

        }  

        public function setColor($color) {

            $this->color = $color;

        }  

        public function describe() {

            return sprintf("GeeksforGeeks %s %s\n",

                $this->getColor(), get_class($this));

        }  

    }

    $triangle = new Triangle();

    $triangle->setColor('green');

    print $triangle->describe();

    ?>

    Output: 
     

    GeeksforGeeks green Triangle

    Importance of using Interfaces: 
     

    • Interface provide a flexible base/root structure that you don’t get with classes.
    • By implementing interface the caller of the object need to care only about the object’s interface not implementations of the object’s methods.
    • Interface allows unrelated classes to implement the same set of methods regardless of their positions in the class inheritance hierarchy.
    • Interface enables you to model multiple inheritance.

    Why do we use interfaces?

    Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.

    Where do we use interface in PHP?

    Interface are definitions of the public APIs that classes (implementing an interface) must implement. It is also known as contracts as an interface allows to specify a list of methods that a class must implement.

    Why are interfaces important in OOP?

    Interfaces are useful in object-oriented programming because they specify the behavior that an object must implement. An interface is a contract between an object and its code. If an object implements an interface, it is guaranteed to support the behavior specified by that interface.