Correct use of PHP interface class interface_PHP tutorial

WBOY
Release: 2016-07-15 13:31:53
Original
732 people have browsed it

For those who are beginners

In fact, their function is very simple. When many people develop a project together, they may all call some classes written by others. Then you They will ask, how do I know how to name the implementation method of a certain function? At this time, the PHP interface class interface comes into play. When we define an interface class, the method inside it is as follows: Classes must be implemented, such as:

  1. interface Shop
  2. {
  3. public function buy($gid);
  4. public function sell($gid);
  5. public function view($ gid);
  6. }

I declare a shop interface class and define three methods: buy, sell , look (view), then all subclasses that inherit this class must implement any of these three methods. If the subclass does not implement these, it will not run. 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:

<ol class="dp-xml">
<li class="alt"><span><span>class BaseShop implements Shop  </span></span></li>
<li><span>{  </span></li>
<li class="alt"><span>public function buy($gid)  </span></li>
<li><span>{  </span></li>
<li class="alt"><span>echo('你购买了ID为 :'.$gid.'的商品');  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>public function sell($gid)  </span></li>
<li><span>{  </span></li>
<li class="alt"><span>echo('你卖了ID为 :'.$gid.'的商品');  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>public function view($gid)  </span></li>
<li><span>{  </span></li>
<li class="alt"><span>echo('你查看了ID为 :'.$gid.'的商品');  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
</ol>
Copy after login

Think about it, in a large project where many people are cooperating, it is so convenient to have interface classes, so that you don’t have to ask others, your What is the method name of a certain function? 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 subclasses must complete its specified method.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446183.htmlTechArticleFor those who are new to it, their function is actually very simple. When many people develop a project together, they may all call it Some categories written by others, then you will ask, how do I know his...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template