Home > Article > Backend Development > Introduction to PHP interface classes and abstract classes_PHP tutorial
1. PHP interface classes: interface
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 will ask, how do I know one of his How is the function implementation method named? At this time, the php interface class comes into play. When we define an interface class, the methods in it must be implemented by the following subclasses, such as:
php interface And abstract class php interface and abstract class Code
interface Shop
{
public function buy($gid);
public function sell($gid);
public function view($gid) ;
}
I declared a shop interface class and defined three methods: buy, sell, and view. Then all subclasses that inherit this class must implement these three methods. Even one less method will not work. 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:
php interface and abstract class php interface and abstract class Code
class BaseShop implements Shop
{
public function buy($gid)
{
echo(you Purchased the product with the ID:.$gid.);
}
public function sell($gid)
{
echo(You sold the product with the ID:.$gid.);
}
public function view($gid)
{
echo(You viewed the product with ID:.$gid.);
}
}
You want Think about it, in a large project where many people collaborate, it is so convenient to have interface classes, so that 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, pointing out the direction, and the subclass must complete its designated method.
2. PHP abstract class: abstract
In fact, abstract classes and interface classes are partly similar. Remember where you saw such a sentence, abstract class extracts the similar part. This sentence looks like It's funny. 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 writing repeatedly, then you can consider using abstract classes. You may Said "Isn't it possible for me to rewrite a class and instantiate each public class and call the same method?" It is possible here. In fact, this is what abstract classes do, but they omit it. This step of instantiation is eliminated, making it as convenient as calling the method of this class directly, and you can also overload this method. Such as:
php interface and abstract class php interface and abstract class Code
abstract class BaseShop
{
public function buy($gid)
{
echo(you buy The product with the ID:.$gid.);
}
public function sell($gid)
{
echo(You sold the product with the ID:.$gid.);
}
public function view($gid)
{
echo(You viewed the product with 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);
}
}
Here is an example. Like the above, I defined a store class and extracted it. All its similar parts, buy, sell, and 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 thing Something unique that introduces duplication of code and improves reusability.