Explain how to use implements in php, and explain implements

Source: Internet
Author: User

Explain how to use implements in php, and explain implements

The application Keywords of interfaces in php are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. implements mainly refer to class names, the Methods owned by the class, as well as the constraints and specifications of the passed parameters, are a bit like abstract class.

Application of interfaces in the class

1. Keyword: interface

2. Keyword: implements

1. Interface introduction and Creation

Interface: A special abstract class in which all member attributes are abstract or constants.

Rules:

1. All classes are abstract methods.

2. abstract methods do not need to be added.

3. The interface abstract method property is public.

4. The member attribute must be a constant.

The format code is as follows:

Interface demo {const NAME = "constant object attributes"; function myfun1 (); // abstract method function myfun2 (); // abstract method, no specific writing logic}

1. Interface Definition and call

<? Phpinterface Cinema {const film = 'Pirates of the Caribbean '; public function show ();} class Order implements Cinema {public function show () {echo "Cinema interface opened <br>" ;}}$ face = new Order (); echo $ face-> show (); echo Cinema: film;

Note: In the preceding example, the method name of the interface is show. The show method must exist in the inherited interface class. Otherwise, an error is reported. That is to say, the interface method is false. What actually works is the method in the inherited class. Is the interface a bit like the php abstract class here?

2. Strict parameter Constraints

<? Phpinterface Cinema {public function show (Order $ show, $ num);} // display the normal class Order implements Cinema {public $ number = '123 '; public function show (Order $ show, $ num) {echo $ show-> number. $ num ;}$ face = new Order (); $ face-> show (new Order, $ num = '3 persons '); // output 3 persons in 0011 rows

Note: The preceding example inherits the interface class. When calling an interface method, the passed parameters must be the same as the parameter names in the interface. Otherwise, an error is reported.

3. inheritance between interfaces and parameters passed by calling Interfaces

<? Phpinterface Cinema {public function show ();} interface Cinema1 extends Cinema {public function show1 (Order1 $ object, $ num);} class Order implements Cinema1 {public function show () {echo "Ready <br>";} public function show1 (Order1 $ object, $ num) {// var_dump ($ object); echo $ object-> number. "$ num <br>" ;}} class Order1 {public $ number = "0012"; function fun () {echo '==========================' ;}}$ show = new Order1; $ show-> fun (); $ test = new Order (); $ test-> show (); $ test-> show1 ($ show, $ num = '6 people '); // output ======================= ready, 0012 rows, 6 persons

Note: The preceding example shows that interface Cinemal1 inherits interface Cinemal and class Order inherits interface cinemal1. I don't know if you find that there are two methods in class Order: show, show1, and no less. If there is one fewer, a fatal error is reported. In show1 (Order1 $ object, $ num), Order1 must be the same as class order1. If they are different, a fatal error is also reported. What if an interface is inherited by multiple classes with different class names? So we need to use self, which will be mentioned below

4. Multiple inheritance for one interface

<? Phpinterface demo {const NAME = "movie NAME"; function fun1 (); function fun2 ();} interface demo2 {function fun3 (); function fun4 ();} interface demo3 {const TEST = "Test"; function fun5 ();} class MyDemo implements demo, demo2 {function fun1 () {echo "hello ";} function fun2 () {echo "----------";} function fun3 () {echo "mE <br/>";} function fun4 () {echo "<br/>" ;}} class YourDemo extends MyDemo implements demo3 {function fun5 () {echo "inherit class and reference interface ";}} $ p = new YourDemo; $ p-> fun1 (); $ p-> fun2 (); $ p-> fun3 (); $ p-> fun4 (); $ p-> fun5 ();

Above output

Hi ---------- me too

Everybody

Reference interface After inheriting class

In the above example, we can see that all interfaces are defined using the keyword interface, and the keyword implements is used to implement the methods in the interface. Another example is as follows:

<? Php // define interface User {function getDiscount (); function getUserType ();} class VipUser implements User {// VIP User interface implements private $ discount = 0.8; // VIP user discount coefficient function getDiscount () {return $ this-> discount;} function getUserType () {return "VIP user ";}} class Goods {var $ price = 88; var $ vc; function run (User $ vc) {// define User interface type parameters, at this time, you do not know the user $ this-> vc = $ vc; $ discount = $ this-> vc-> getDiscount (); $ usertype = $ this-> vc-> getUserType (); echo $ usertype. "product price :". $ this-> price * $ discount; }}$ display = new Goods (); $ display-> run (new VipUser); // VIP user product price: 70.4

This example demonstrates a simple application of a PHP interface. In this example, the User interface achieves the User discount, while the VipUser class implements the specific discount coefficient. Finally, Goods provides different User quotations based on the User interface.

Summary:

Differences between abstract classes and interfaces

An interface is a special abstract class and can also be seen as a model specification. The interface and abstract class are roughly different as follows:

1. If a subclass implements an interface, all methods in the interface must be implemented (whether or not required); if an abstract class is inherited, you only need to implement the required methods.

2. if the method name defined in an interface is changed, all subclasses implementing this interface must synchronously update the method name. If the method name in the abstract class is changed, the method name corresponding to its subclass is not affected, but is changed to a new method (relatively old method implementation ).

3. abstract classes can only be inherited. Interfaces must be used when a subclass needs to implement functions inherited from multiple parent classes.

The above section describes how to use implements in php. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.