Related Articles

Related Articles

When do we need Interfaces in PHP?
  • Last Updated : 03 Nov, 2018

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
interface man {
  
   // List of public methods
  
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
interface IMyInterface {
    const INTERFACE_CONST_1 = 1;
    const INTERFACE_CONST_2 = 'a string';
    
    public function method_1();
    public function method_2();
}
?>

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Function to Imlements more than one 
// interface by single class.
   
class MyClass implements IMyInterface {
   
    public function method_1() {
   
        // method_1  implementation 
    }
  
    public function method_2() {
   
        // method_2 implementation
    }
}

chevron_right


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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    interface Foo {
      
    }
    interface Bar {
      
    }
    interface Bass extends Foo, Bar {
      
    }

    chevron_right

    
    

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to Implement interface.
   
// Define a new Interface for all 'shapes' 
// to inherit
interface Shape {
    
    // Define the methods required for
    // classes to implement
    public function getColor();
    public function setColor($color);    
      
    // Interfaces can't define common 
    // functions so we'll just define a 
    // method for all implementations to
    // define
    public function describe();
}
  
// Define a new 'Triangle' class that 
// inherits from the 'Shape' interface
   
class Triangle implements Shape {
    
    private $color = null;
      
    // Define the required methods defined 
    // in the abstractclass 'Shape'
    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();
  
// Set the color
$triangle->setColor('green');
  
// Print out the value of the describe common 
// method provided by the abstract class will
// print out out "I am an Orange Triange"
print $triangle->describe();
?>

chevron_right


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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :