Every time I learn about interfaces and abstractions, I get confused and don’t understand the differences and uses between them. Today I will move the book and sort out my ideas. I hope it will be helpful to friends who are new to learning PHP.
1.php interface class: interface
In fact, their role 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 how to name the implementation method of a certain function? Well, 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:
The code is as follows:
Interface Shop
{
public function buy($gid);
public function sell($gid);
public function view($gid);
}
I declare a shop interface class and define 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 words, 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, such as:
The code is as follows:
class BaseShop implements Shop
{
public function buy($gid)
{
echo('You purchased the product with 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.');
}
}
Think about it, in a large project where many people collaborate, it is very 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 There's nothing I can do about 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 are partly similar to interface classes. The function of abstract classes is that when you find that many of your classes use many methods that you are constantly writing repeatedly, then you can consider using abstract classes. You may People will say, "Isn't it possible for me to rewrite a class and instantiate one public class for each public class and call the same method?" It is possible here. In fact, this is what abstract classes do, but it saves the effort. Without the step of instantiation, it is as convenient as calling the method of this class directly, and you can also overload this method. Such as:
The code is as follows:
abstract class BaseShop
{
public function buy($gid)
{
echo('You purchased the product with 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, extracted all its parts, buy, sell, and view, and implemented these methods 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 code duplication, and improve reusability.
Finally, to summarize: 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 also do it yourself. Do.