PHP interface and abstract learning instance
Every time you learn interfaces and abstractions, you will be confused about the differences and usage between them. Today, I moved my books and thought out my ideas, hoping to help my new friends who are learning PHP.
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, abstract classes and interface classes are similar. abstract classes are used to repeatedly write many of your classes, then you can consider using abstract classes. You may say, "I am not allowed to rewrite a class. Each public class will instantiate a public class and call the same method ", here we can, in fact, the work of the abstract class is also this, But it saves the step of your instantiation, making it as convenient as directly calling this class method, 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.
The abstract class is a class service provider. It has many services and you don't have to use them. You can use them when you need them. If you don't feel satisfied with the service, you can also do it yourself.