Home  >  Article  >  Backend Development  >  what is the interface in php

what is the interface in php

(*-*)浩
(*-*)浩Original
2019-09-11 14:51:503860browse

Interface interface is a stipulation, something that people can inherit from, a bit like an abstract class

what is the interface in php

is defined in it method, but it is not instantiated, but requires other classes to implement it, and all methods defined by the interface must be implemented one by one.

For example(Recommended learning: PHP programming from entry to proficiency)

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

I declare a shop interface class and define There are three methods: 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 will not work. 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 one less will work, but how do you do it specifically? I don't care, that's your business, such as:

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

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

Statement:
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