Php object interface

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.