What is Interface in PHP OOPS ?

Interface in oop enforce definition of some set of method in the class. 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. Inter

Interface in PHP

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 an interface in PHP using interface keyword. Rest of the things are typically identical to classes. Following is a very small example of an interface in PHP.

interface abc
 {
 public function xyz($b);
 }

So in above code, you are creating an interface with name abc. Interface abc has function xyz. Whenever you will implement the abc interface in your class then you have to create a method with the name xyz. If you will not create function xyz then it will throw an 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 a method in the interface with public accessibility. If you will use other than public visibility in the interface then it will throw an error. Also while defining method in your interface does not use the abstract keyword in your methods.

You can also extend interface like class. You can extend the interface in PHP using extends keyword.

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
 }
 }

So here template2 has all property of tempate2. So whenever you will implement template2 in your class, you have to create function of both interfaces.

You can also extend multiple interfaces 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
 }
 }

 

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
 }
 }

You can not implement 2 interfaces if both share function with the same name. It will throw an 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;
 }
 }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: