Correct use of PHP interface interfaces _ PHP Tutorial

Source: Internet
Author: User
Use PHP interface correctly. For beginners, their role is very simple. when many people develop a project together, they may call some classes written by others. then you will ask, how do I know about some of his beginners?

In fact, their role is very simple. when many people develop a project together, they may call some classes written by others, and you will ask, how do I know how the implementation method of a function is named? at this time, the PHP interface class interface plays a role. when we define an interface class, the method in it must be implemented by the following sub-classes, for example:

  1. Interface Shop
  2. {
  3. Public function buy ($ gid );
  4. Public function compute ($ gid );
  5. Public function view ($ gid );
  6. }

I declare a shop interface class and define three methods: buy, sell, view ), therefore, none of the three methods must be implemented for all subclasses that inherit this class. if the subclass does not implement these methods, it will not be able to run. Actually, an interface class is a class template and a class rule. if you belong to this class, you must follow my rules, but how do you do it? I don't care. it's your business, for example:

 
 
  1. Class BaseShop implements Shop
  2. {
  3. Public function buy ($ gid)
  4. {
  5. Echo ('item with ID: '. $ gid.' You purchased ');
  6. }
  7. Public function compute ($ gid)
  8. {
  9. Echo ('item with ID: '. $ gid.' You sold ');
  10. }
  11. Public function view ($ gid)
  12. {
  13. Echo ('The item with ID: '. $ gid.' You viewed ');
  14. }
  15. }

You can think about how convenient it is to have an interface class in a large project that has many people working together, so you don't have to ask others, what is the method name of your xx function, of course, if you like this, I can't.

Conclusion: The PHP interface class interface is a class leader. it specifies the direction and the subclass must specify the method.


In fact, they have a very simple role. when many people develop a project together, they may call some classes written by others. then you will ask, how do I know his...

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.