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:
- Interface Shop
- {
- Public function buy ($ gid );
- Public function compute ($ gid );
- Public function view ($ gid );
- }
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:
- Class BaseShop implements Shop
- {
- Public function buy ($ gid)
- {
- Echo ('item with ID: '. $ gid.' You purchased ');
- }
- Public function compute ($ gid)
- {
- Echo ('item with ID: '. $ gid.' You sold ');
- }
- Public function view ($ gid)
- {
- Echo ('The item with ID: '. $ gid.' You viewed ');
- }
- }
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...