Skip to main content

Interface In PHP

  • An interface is similar to a class except that it cannot contain code.
  • An interface can define method names and arguments, but not the contents of the methods.
  • Any classes implementing an interface must implement all methods defined by the interface.
  • A class can implement multiple interfaces.
  • An interface is declared using the "interface" keyword.
  • Interfaces can't maintain Non-abstract methods.

Example 1

  1. <?php  
  2.     interface a  
  3.     {  
  4.         public function dis1();  
  5.     }  
  6.     interface b  
  7.     {  
  8.         public function dis2();  
  9.     }  
  10.   
  11. class demo implements a,b  
  12. {  
  13.     public function dis1()  
  14.     {  
  15.         echo "method 1...";  
  16.     }  
  17.     public function dis2()  
  18.     {  
  19.         echo "method2...";  
  20.     }  
  21. }  
  22. $objnew demo();  
  23. $obj->dis1();  
  24. $obj->dis2();  
  25.   
  26. ?>  

 

Example 2

  1. <?php  
  2.     interface i1  
  3.     {  
  4.         public function fun1();  
  5.     }  
  6.     interface i2  
  7.     {  
  8.         public function fun2();  
  9.     }  
  10. class cls1 implements i1,i2  
  11. {  
  12.     function fun1()  
  13.     {  
  14.         echo "javatpoint";  
  15.     }  
  16.     function fun2()  
  17.     {  
  18.         echo "SSSIT";  
  19.     }  
  20. }  
  21. $objnew cls1();  
  22. $obj->fun1();  
  23. $obj->fun2();  
  24.   
  25. ?>  

Comments