Introduction
Interface is an important feature of object oriented programming by which it is possible to specify methods to be implemented by a class, without having to define how they should be implemented.
PHP supports interface by way if interface keyword. Interface is similar to class but with methods without definition body. Methods in interface must be public. An inherited class that implements these methods must be defined with implements keyword instead of extends keyword, and must provide implementations of all methods in parent interface.
Syntax
<?php interface testinterface{ public function testmethod(); } class testclass implements testinterface{ public function testmethod(){ echo "implements interfce method"; } } ?>
All methods from interface must be defined by the implementing class, otherwise PHP parser throws exception
Example
<?php interface testinterface{ public function test1(); public function test2(); } class testclass implements testinterface{ public function test1(){ echo "implements interface method"; } } $obj=new testclass() ?>
Output
The error is as shown below −
PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)
Extendable interface
Just as a normal class, an interface can also be inherited from, using extends keyword.
In following example, parent class has two abstract methods, only one of which is redefined in child class. This results in error as follows −
Example
<?php interface testinterface{ public function test1(); } interface myinterface extends testinterface{ public function test2(); } class testclass implements myinterface{ public function test1(){ echo "implements test1 method"; } public function test2(){ echo "implements test2 method"; } } ?>
Multiple inheritance using interface
PHP doesn't allow more than one class in extends clause. However, multiple inheritance can be achieved by letting child class implementing one or more interfaces
In following example, myclass extends testclass and implements testinterface to achieve multiple inheritance
Example
<?php interface testinterface{ public function test1(); } class testclass{ public function test2(){ echo "this is test2 function in parent class\n"; } } class myclass extends testclass implements testinterface{ public function test1(){ echo "implements test1 method\n"; } } $obj=new myclass(); $obj->test1(); $obj->test2(); ?>
Output
This will produce following output −
implements test1 method this is test2 function in parent class
Interface example
Example
<?php interface shape{ public function area(); } class circle implements shape{ private $rad; public function __construct(){ $this->rad=5; } public function area(){ echo "area of circle=" . M_PI*pow($this->rad,2) ."\n"; } } class rectangle implements shape{ private $width; private $height; public function __construct(){ $this->width=20; $this->height=10; } public function area(){ echo "area of rectangle=" . $this->width*$this->height ."\n"; } } $c=new circle(); $c->area(); $r=new rectangle(); $r->area(); ?>
Output
Above script produces following result
area of circle=78.539816339745 area of rectangle=200