Interface
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.
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 interface in php using interface keyword. Rest of the things are typically identical to classes. Following is very small example of interface in php.
1 2 3 4 |
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
1 2 3 4 5 6 7 |
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 extends keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 interface in one interface in php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 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
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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
1 2 3 4 5 6 7 8 9 10 11 |
interface template1 { public function f1($a = 20) } class test implements template1 { public function f1($name = "ankur") { echo $name; } } |