Interface in php

Page 1

Interface in PHP Created By:

www.creativedev.in


INTERFACE Introduction • Interfaces allow you to define a some standard structure for your classes. Interface helps to create multiple inheritance because as most of developers knows, PHP does not support multiple inheritance directly, we need to use interface to implement multiple inheritance which is not supported with abstract. • Interface can be, not extended. Interface can contain abstract method. If Interface variables are constant or final then it must be initialized. implemented by class


SYNTAX access_specifier Interfacee interface_name //access_specifier is public or no_modifier { return_type methodname1(arg list); // Method declaration return_type methodname2(arg list); type variable_name1 = value1; // Variable declaration type variable_name2 = value2 ‌.......... ‌....... } // Interface created class Class_name implements interface_name{ public function function_name(){ // Desired method code } }


Continue...


Examples interface intI{ function disp(); } class Cls implements intI{ public function disp(){ echo "inteface intI implemented by Cls"; } } $obj = new Cls(); $obj->disp(); // prints “inteface intI implemented by Cls�


Extended Interface interface I1{ function m1(); } interface I2 extends I1{ function m2(); } class C1 implements I1{ public function m1(){ echo "Method M1<br/>"; } public function m2(){ echo "Method M2"; } } $obj = new C1(); $obj->m1(); $obj->m2(); //Prints //Method M1 //Method M2


Finally, The difference between class and interface Class  We can extend the class.  Multiple inheritance is not supported by class. Interface  We can implement the interface.  Interface can support multiple inheritance.


Thanks You If you have any query, contact us on info@creativedev.in www.creativedev.in


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.