Home >Backend Development >PHP Tutorial >How to correctly use php interface class interface
For those who are new to the PHP language, they may not know much about the interface class of PHP. Next, we will explain in detail how to use the PHP interface class interface.
How to correctly use PHP XMLReader to parse XML documents
In-depth interpretation of PHP DOMXPathParsing XML files
Recommend several powerful PHPTemplate engine
Key points analysis of PHP usage skills
How to correctly use PHP DOM-XML to create XML files
In fact, their function is very simple. When many people develop a project together, they may call some classes written by others. Then you may ask, how do I know how to name the implementation method of a certain function? At this time, the PHP interface class interface plays a role. It works. When we define an interface class, the methods in it must be implemented by the following subclasses. For example:
interface Shop { public function buy($gid); public function sell($gid); public function view($gid); }
declares a shop interface class and defines three methods: buy (buy) , sell (sell), see (view), then Inherit All subclasses of this class must implement any of these three methods. If the subclass does not implement these, it will not work. In fact, the interface class is, to put it bluntly, the template of a class and the regulations of a class. If you belong to this category, you must follow my regulations. No matter how you do it, I don’t care how you do it. That’s up to you. Things like:
class BaseShop implements Shop { public function buy($gid) { echo('你购买了ID为 :'.$gid.'的商品'); } public function sell($gid) { echo('你卖了ID为 :'.$gid.'的商品'); } public function view($gid) { echo('你查看了ID为 :'.$gid.'的商品'); } }
Think about it, in a large project where many people are working together, how convenient it is to have an interface class, so that you don’t have to ask others about your method of a certain function. What’s the name? Of course, if you like this, I can’t help it.
Conclusion: The PHP interface class interface is the leader of a class, indicating the direction, and the subclass must complete its designated method.
<?php interface Shop { public function buy($gid); public function sell($gid); public function view($gid); } class BaseShop implements Shop { public function buy($gid) { echo('你购买了ID为 :'.$gid.'的商品'); } public function sell($gid) { echo('你卖了ID为 :'.$gid.'的商品'); } public function view($gid) { echo('你查看了ID为 :'.$gid.'的商品'); } } $haha = new BaseShop(); $haha->buy('123');
The above is the detailed content of How to correctly use php interface class interface. For more information, please follow other related articles on the PHP Chinese website!