Interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented. (the methods don't have a body )
The interface define a generic template and separates the implementation and defines the structure.
Interfaces can be defined with the interface
keyword, and without any of the methods having their contents defined.
All methods declared in an interface must be public.
<?php // Declare the interface 'Phone' interface Phone { public function sendSMS($to,$msg); // must be public public function makeACall($to); // must be public }
Abstract Class
and Interfaces
one
abstract classmany
interfacesextends
abstract classimplement
interfaceAll methods in the interface must be implemented within a class.
(If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract)
we use the keyword implements
to implement an interface:
<?php // Implement the interface class LauPhone implements Phone { public function sendSMS($to,$msg) { } public function makeACall($to) { } }
If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract.
<?php // If it is not declared `abstract`: // Fatal error: Class WatchPhone contains 1 abstract method and must // therefore be declared abstract or implement the remaining methods (Phone::sendSMS) abstract class WatchPhone implements Phone { public function makeACall($to) { } }
the method definitions must be provided by the subclass:
class MyPhone extends WatchPhone{ public function sendSMS($to,$msg){ echo $to,$msg; } public function makeACall($to){ echo $to; } } $t = new MyPhone(); $t->sendSMS("lautturi","hello");
Interfaces can be extended like classes using the extends operator.
<?php Interface IF1 { public function method1(); } Interface IF2 extends IF1 { public function method2(); } class MyClass implements IF1 { public function method1() { echo "Method1 in MyClass.\n"; } } $myclass = new MyClass(); $myclass -> method1(); class MyClass2 implements IF2 { public function method1() { echo "Method1 in MyClass2.\n"; } public function method2() { echo "Method2 in MyClass2.\n"; } } // both method1 and method2 must be implemented in MyClass2. $myclass2 = new MyClass2(); $myclass2 -> method1(); $myclass2 -> method2(); ?>
Classes may implement multiple interfaces by separating each interface with a comma.
Notice: Prior to PHP 5.3.9, the interfaces could not have method with the same name.
There are two way to implemente multiple interfaces
<?php interface IF1 { public function method1(); } interface IF2 { public function method2(); } interface IF3 extends IF1, IF2 { public function method3(); } class MyClass implements IF3 { public function method1() { echo "Method1.\n"; } public function method2() { echo "Method2.\n"; } public function method3() { echo "Method3.\n"; } } $obj = new MyClass(); $obj -> method1(); $obj -> method2(); $obj -> method3(); ?>
<?php Interface IF1 { public function method1(); } Interface IF2 { public function method2(); } class MyClass implements IF1, IF2 { public function method1() { echo "Method1.\n"; } public function method2() { echo "Method2.\n"; } } $obj = new MyClass(); $obj -> method1(); $obj -> method2(); ?>
A class can implement interfaces while extend a class.
<?php Interface MyInterface { public function method1(); } class ParentClass { public function method2(){ } public function method3(){ } } class ChildClass extends ParentClass implements MyInterface { // Must implement the method1 function method1() { echo "method1 in ChildClass."; } function method3() { echo "override method3"; } } $child = new ChildClass(); $child -> method1(); ?>