what is interface?

An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.

To extend from an Interface, keyword implements is used.

We can have a class extend from more than one Interface

By implementing interface you are forcing any class to must declaring some specific set of methods in oop. For example if you are creating class to render HTML element then it is necessary to set id and name of your html tag. So in this case you will create interface for that class and define method like setID and setName. So whenever someone will create any class to render HTML tag and implemented your interface then he must need to define setId and setName method in their class. In other word you can say that by help of interface you can set some definition of your object. Interface is very useful if you are creating architecture of any oop base application.


Interface in php can be implemented like other oop lanugage. You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement.
You can create interface in php using interface keyword. Rest of the things are typically identical to classes. Following is very small example of interface in php.
interface abc{
public function xyz($b);
}


So in above code you are creating interface with name abc. Interface abc has function xyz. Whenever you will implement abc interface in your class then you have to create method with name xyz. If you will not create function xyz then it will throw error.
You can implement your interface in your class using implements keyword. Let us implement our interface abc in our class.
class test implements abc{
public function xyz($b){
//your function body
}
}


You can only define method in interface with public accessibility. If you will use other than public visibility in interface then it will throw error. Also while defining method in your interface do not use abstract keyword in your methods. You can also extend interface like class. You can extend interface in php using extendskeyword.

File name : index.php

interface template1{
public function f1();
}
interface template2 extends template1{
public function f2();
}
class abc implements template2{
public function f1()
{
//Your function body
}
public function f2(){
//your function body
}
}

File name : index.php

You can also extend multiple interface in one interface in php.
interface template1{
public function f1();
}
interface template2{
public function f2();
}
interface template3 extends template1, template2{
public function f3();
}
class test implements template3{
public function f1(){
//your function body
}
public function f2(){
//your function body
}
public function f3(){
//your function body
}
}

File name : index.php

You can also implement more than one interface in php class.
interface template1{
public function f1();
}
interface template2{
public function f2();
}
class test implments template1, template2{
public function f1()
{
//your function body
}
public function f2(){
//your function body
}
}

File name : index.php

You can not implement 2 interfaces if both share function with same name. It will throw error.
Your function parameter in class must be identical to the parameter in the interface signature. Following is example some example
interface template1{
public function f1($a)
}
class test implements template1{
public function f1($a)
{
echo $a;
}
}

Above will work. But following example will not work:
interface template1{
public function f1($a)
}
class test implements template1{
public function f1()
{
echo $a;
}
}


But it is not necessary to use the same name of the variable. Like $a. You can also use any name. For example:
interface template1{
public function f1($a)
}
class test implements template1{
public function f1($name)
{
echo $name;
}
}


If you are using default argument then you can change your value of the argument. For example
interface template1{
public function f1($a = 20)
}
class test implements template1{
public function f1($name = "ankur")
{
echo $name;
}
}
In above section we have discussed interfaces and abstract classes in php. Both are almost doing same things but has some difference.

Differences between abstract class and interface in PHP
Following are some main difference between abstract classes and interface in php
In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.
Method of php interface must be public only. Method in abstract class in php could be public or protected both.
In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

File name : index.php

interface Storable{
function getContentsAsText();
}
class Document implements Storable{
public function getContentsAsText(){
return “This is Text of the Documentn”;
}
}
class Indexer {
public function readAndIndex(Storable $s) {
$textData = $s->getContentsAsText();
//do necessary logic to index
echo $textData;
}
}
$p =new Document();
$i =new Indexer();
$i->readAndIndex($p);

In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed


Difference between Abstract Class and Interface
Abstract Classes
An abstract class can provide some functionality and leave the rest for derived class The derived class may or may not override the concrete functions defined in base class The child class extended from an abstract class should logically be related
Interface
An interface cannot contain any functionality. It only contains definitions of the methods The derived class must provide code for all the methods defined in the interface Completely different and non-related classes can be logically be grouped together using an interface





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here