PHP  interfaces

php interfaces are an excellent way to implement reusability.

php interface is a collection of abstract methods and constants.

an interface method are public and abstract by default so we do not need to specifies these attributes.

all methods declared in an interface must be public.

php interfaces are defined using the interface keyword. look below code:

      interface    php_interface{
                                         const    product_id = "232kf"; 
                                         public   function   set_value(); //method declaration 

you can implement your interface in your class using implements keyword.

if a class implements an interface then it must define all the interface methods, if any of the method remain undefined in the class then class must be declared abstract otherwise it generate error.

the class implementing the interface must use the exact same method signatures as are defined in the interface.

note :- you cannot create an instance of an interface. interface can't contain properties, they can only contain method declarations and constants.

         interface    Php_interface{
                         const   php_interface_id = "4f4k3";
                         public  function   set_interface_color( $cc );
                         public  function   set_interface_price( $pp );
                    function   php_interface_value(); //show method by default public
            class     php_data   implements   Php_interface{
                             private   $php_interface_color = null;
                             private   $php_interface_price = null;
  public    function     set_interface_color( $cc ){     
                                           $this->php_interface_color = $cc ; } 
  public    function     set_interface_price( $pp ){  
                                           $this->php_interface_price = $pp ; }
  public    function    php_interface_value(){ 
                        echo  "php interface color = ". $this->php_interface_color;
                        echo  "<br/> php interface price = ". $this->php_interface_price;
         // $i_obj =  new  php_interface();  you cannot instantiate  an interface 
                        $ppinterface  = new  php_data();
                        $ppinterface->set_interface_color( "green" );
                        $ppinterface->set_interface_price( "$445454" );
                        echo "<br/>".php_interface::php_interface_id;
                        echo "<br/>".php_data::php_interface_id;   


your function parameter in class must be identical to the parameter in the interface signature.

note : a class can implement more then one interfaces in php.

note : you can not implement two interfaces if both share function with same name. it will throw an error.

            interface    automobile{
                         const   product_id = "43fk33";
                         public  function   set_company( $cc );
    interface    setdata{
                          public  function   set_data_color( $ccc );
                           function   set_data_price( $ppp ); //show method by default public
                            function   display_data();                                     

    class   php_main_interface   implements   automobile, setdata{
                             private   $company = null;
                             private   $color = null;
                             private   $price = null;
         public    function    set_company ( $com ){  $this->company = $com;}          
         public    function    set_data_color( $ccc){  $this->color = $ccc ;} 
         public    function    set_data_price( $ppp ){   $this->price = $ppp ;}
         public    function    display_data(){  
                        echo   "<br/>php_main_interface company = ". $this->company;
                        echo  "<br/> php_main_interface color = ". $this->color;
                        echo  "<br/> php_main_interface price = ". $this->price;
         // $i_obj =  new  automobile();  you cannot instantiate  an interface 
                        $maininterface  = new  php_main_interface();
                        $maininterface->set_company( "interfacedata" );
                        $maininterface->set_data_color( "green" );
                        $maininterface->set_data_price( "$445454" );
                        echo "<br/>".automobile::product_id;
                        echo "<br/>".php_main_interface::product_id;   


extending interfaces in PHP:

an interface can extend other interfaces, using the extends clause.

an interface can extend several interfaces.

       interface    one{  public  function  show();  }
       interface    Two  extends  one{  public  function  how( data  $da );}
       class    access  implements  two{
                public    function  show(){
                                             echo  "this is show method";
                public    function  how( data $da ){
                                             echo  "this is  how method " ;