Many people cannot figure out the differences between interfaces and abstract classes! I will share my understanding with you! 1. php interface class: interface
In fact, their role is very simple. when many people develop a project together, they may call some classes written by others, and you will ask, how do I know how the implementation method of a function is named? at this time, the php interface class plays a role. when we define an interface class, the method in it must be implemented by the following sub-classes, for example:
The code is as follows:
Interface Shop
{
Public function buy ($ gid );
Public function compute ($ gid );
Public function view ($ gid );
}
I declare a shop interface class and define three methods: buy, sell, view ), therefore, none of the three methods must be implemented for all subclasses that inherit this class. if the subclass does not implement these methods, it will not be able to run. Actually, an interface class is a class template and a class rule. if you belong to this class, you must follow my rules, but how do you do it? I don't care. it's your business, for example:
The code is as follows:
Class BaseShop implements Shop
{
Public function buy ($ gid)
{
Echo ('item with ID: '. $ gid.' You purchased ');
}
Public function compute ($ gid)
{
Echo ('item with ID: '. $ gid.' You sold ');
}
Public function view ($ gid)
{
Echo ('The item with ID: '. $ gid.' You viewed ');
}
}
You can think about how convenient it is to have an interface class in a large project that has many people working together, so you don't have to ask others, what is the method name of your xx function, of course, if you like this, I can't.
Conclusion: an interface class is the leader of a class. it specifies the direction and the subclass must specify the method.
2. php abstract class: abstract
In fact, the abstract class is very similar to the interface class. I remember where I saw this sentence. the abstract class extracts the class image part. this sentence looks funny. In fact, it tells the truth of the abstract class, the role of an abstract class is that when you find that many of your classes are repeatedly written using many methods, you can consider using an abstract class, you may say, "I can't rewrite a class, each public class, I instantiate a public class, and you can call the same method, in fact, this is the work of the abstract class, but it saves you the step of instantiation, making it as convenient as directly calling this class method, and you can also reload this method. For example:
The code is as follows:
Abstract class BaseShop
{
Public function buy ($ gid)
{
Echo ('item with ID: '. $ gid.' You purchased ');
}
Public function compute ($ gid)
{
Echo ('item with ID: '. $ gid.' You sold ');
}
Public function view ($ gid)
{
Echo ('The item with ID: '. $ gid.' You viewed ');
}
}
Class BallShop extends BaseShop
{
Var $ itme_id = null;
Public function _ construct ()
{
$ This-> itme_id = 2314;
}
Public function open ()
{
$ This-> require ($ this-> itme_id );
}
}
Here is an example. I want to define a shop class like above, extract all its image parts, buy, sell, and view ), and abstract classes all implement these methods, so the child classes that inherit it will automatically obtain these methods, and the child classes will do their own unique things, introduce code duplication, and improve reusability.
Conclusion: an abstract class is a service provider of a class. it has many services that you don't need to use. you can use it when you need it. if you don't feel satisfied with the service, you can also do it yourself.
The above is my opinion on the php interface class and abstract class. I hope I can help these two friends. if you have any comments, please leave a message!