Home > Backend Development > PHP Tutorial > What are the functions of php interface classes and abstract classes?

What are the functions of php interface classes and abstract classes?

伊谢尔伦
Release: 2023-03-12 12:16:02
Original
2630 people have browsed it

1.php InterfaceClass:interface

In fact, their role is very simple. When many people develop a project together, they may all call others If you write some classes, 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, in it The method must be implemented by the following subclasses, for example:

The code is as follows:

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


I declare a shop interface class and define three methods: buy( buy), sell (sell), and see (view), then InheritanceAll 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, such as:

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

Think about it, in a large project where many people collaborate, how convenient it is to have an interface class, so that you can There is no need 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, pointing out the direction, and the subclass must complete its specified method.
2.php Abstract class : abstract
In fact, abstract classes and interface classes are partially similar. Remember where I saw this sentence, abstract classes extract the similar parts. This The sentence looks funny, but in fact it tells the truth about abstract classes. The function of abstract classes is that when you find that many of your classes use many methods that you keep repeating, then you can consider using abstract classes. You may say, "Isn't it possible for me to rewrite a class and instantiate each public class and call the same method?" Yes, it is possible. In fact, this is what abstract classes do, but It saves you the step of instantiation, making it as convenient as calling the method of this class directly, and you can alsooverloadthis method. For example:

The code is as follows:

abstract class BaseShop 
{ 
public function buy($gid) 
{ 
echo('你购买了ID为 :'.$gid.'的商品'); 
} 
public function sell($gid) 
{ 
echo('你卖了ID为 :'.$gid.'的商品'); 
} 
public function view($gid) 
{ 
echo('你查看了ID为 :'.$gid.'的商品'); 
} 
} 
class BallShop 
extends
 BaseShop 
{ 
var $itme_id = null; 
public function construct() 
{ 
$this->itme_id = 2314; 
} 
public function open() 
{ 
$this->sell($this->itme_id); 
} 
}
Copy after login

Here is an example. Like the above, I defined a store class and extracted all its parts like buy and sell. (sell), look (view), and these methods are implemented in the abstract class, then the subclass that inherits it will automatically obtain these methods, and the subclass will do its own unique things, introduce duplication of code, and improve reuse sex.
Conclusion: An abstract class is a service provider of a class. It has many services. You don’t have to use them. You can use them when needed. If you feel dissatisfied with not providing services, you can do it yourself.

The above is the detailed content of What are the functions of php interface classes and abstract classes?. For more information, please follow other related articles on the PHP Chinese website!

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