Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods. An interface is defined by the interface keyword, just like defining a standard class, but all methods are empty. All methods defined in the interface must be public, which is a feature of the interface. Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.
An interface is defined by the interface keyword, just like defining a standard class, but all methods are empty.
All methods defined in the interface must be public, which is a feature of the interface.
Implementation (implements)
To implement an interface, use the implements operator. Class must implement all methods defined in the interface, otherwise a fatal error is reported. Class can implement multiple interfaces, separated by commas.
Note:
When multiple interfaces are implemented, the methods in the interface cannot have duplicate names.
Note:
Interfaces can also be inherited by using the extends operator.
Note:
To implement the interface, you must use the method exactly the same as the method defined in the interface. Otherwise, a fatal error occurs.
Constant
Constants can also be defined in the interface. The use of interface constants and class constants is identical, but they cannot be overwritten by the quilt class or sub-interface.
Example
Example #1 interface Example
Interface ITemplate {public function setVariable ($ name, $ var); public function getHtml ($ template );} // implementation interface // the following code is correct: class Template implements ITemplate {private $ vars = array (); public function setVariable ($ name, $ var) {$ this-> vars [$ name] = $ var;} public function getHtml ($ template) {foreach ($ this-> vars as $ name => $ value) {$ template = str_replace ('{'. $ name. '}', $ value, $ template);} return $ template;} // the following code is incorrect. an error is returned because getHtml () is not implemented () class BadTemplate implements ITemplate {private $ vars = array (); public function setVariable ($ name, $ var) {$ this-> vars [$ name] = $ var ;}}
Example #2 extensible interfaces
Interface a {public function foo ();} interface B extends a {public function baz (Baz $ baz);} // class c implements B {public function foo () {} public function baz (Baz $ baz) {}} // class d implements B {public function foo () {} public function baz (Foo $ foo ){}}
Example #3 inherit multiple interfaces
interface a{ public function foo();}interface b{ public function bar();}interface c extends a,b{ public function baz();}class d implements c{ public function foo(){} public function bar(){} public function baz(){}}
Example #4 use an interface constant
Interface a {const B = 1;} // The constant echo a: B; // class B implements a {const B = 1 ;}
Adding type constraints to interfaces provides a good way to ensure that an object contains some methods. See instanceof operators and type constraints.