Solve the puzzles of "interfaces" and "abstraction" for a long time-Recommending "practical functions of PHP interface classes and abstract classes" [favorites]

Source: Internet
Author: User

 

1. php Interface Class: Interface
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 plays a role. When we define an interface class, the method in it must be implemented by the following sub-classes, for example:

The Code is as follows: Interface shop <br/>{< br/> Public Function buy ($ GID); <br/> Public Function compute ($ GID ); <br/> Public Function view ($ GID); <br/>} 

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:

The Code is as follows: Class baseshop implements shop <br/>{< br/> Public Function buy ($ GID) <br/>{< br/> echo ('your purchased ID is :'. $ GID. 'item '); <br/>}< br/> Public Function compute ($ GID) <br/>{< br/> echo ('your sold ID is: '. $ GID. 'item '); <br/>}< br/> Public Function view ($ GID) <br/>{< br/> echo (' You have viewed the ID: '. $ GID. 'commodity '); <br/>}< br/>}

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: an interface class is the leader of a class. It specifies the direction and the subclass must specify the method.
2. php abstract class: Abstract
In fact, abstract classes and interface classes are similar. abstract classes are used to repeatedly write many of your classes, then you can consider using abstract classes. You may say, "I am not allowed to rewrite a class. Each public class will instantiate a public class and call the same method ", here we can, in fact, the work of the abstract class is also this, But it saves the step of your instantiation, making it as convenient as directly calling this class method, you can also reload this method. For example:

The Code is as follows: Abstract class baseshop <br/>{< br/> Public Function buy ($ GID) <br/>{< br/> echo ('your purchased ID is :'. $ GID. 'item '); <br/>}< br/> Public Function compute ($ GID) <br/>{< br/> echo ('your sold ID is: '. $ GID. 'item '); <br/>}< br/> Public Function view ($ GID) <br/>{< br/> echo (' You have viewed the ID: '. $ GID. 'item '); <br/>}< br/> class ballshop extends baseshop <br/>{< br/> var $ itme_id = NULL; <br/> public function _ construct () <br/>{< br/> $ this-> itme_id = 2314; <br/>}< br/> Public Function open () <br/>{< br/> $ this-> invoke ($ this-> itme_id ); <br/>}< br/>} 

Here is an example. I want to define a shop class like above, extract all its image parts, buy, sell, and view ), and abstract classes all implement these methods, so the child classes that inherit it will automatically obtain these methods, and the Child classes will do their own unique things, introduce code duplication, and improve reusability.
Conclusion: an abstract class is a service provider of a class. It has many services that you don't need to use. You can use it when you need it. If you don't feel satisfied with the service, you can also do it yourself.

 

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.