What does the php interface do?

(*-*)浩
Release: 2023-02-24 06:38:01
Original
4264 people have browsed it

php Interface class: interface

What does the php interface do?

In fact, their role is very simple. When many people develop a project together, they may all call some classes written by others. Then you will ask, how do I know how to name the implementation method of a certain function? At this time, the php interface class comes into play. When we define an interface class, The methods in it The following subclasses must be implemented

The code is as follows: (Recommended learning:PHP programming from entry to proficiency)

interface Shop 
{ 
public function buy($gid); 
public function sell($gid); 
public function view($gid); 
}
Copy after login

I declare a shop interface class , three methods are defined: buy, sell, and view. Then all subclasses that inherit this class must implement any of these three methods. If the subclass does not implement these, It won't work.

ActuallyThe 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, even if one is missing, but I don’t care how you do it, that’s your business. For example:

The code is as follows:

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.'的商品'); 
    } 
}
Copy after login

If you think about it, in a large project with multiple people cooperating, there is an interface. Classes are so convenient, so you don't have to ask others what the method name of your certain function is. Of course, if you like this, I can't help it.

Conclusion: The interface class is the leader of a class, indicating the direction, and the subclass must complete its specified method.

The above is the detailed content of What does the php interface do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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