Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between PHP abstract and interface

Detailed explanation of the difference between PHP abstract and interface

怪我咯
怪我咯Original
2017-07-05 10:36:071355browse

This article is a detailed analysis and introduction to the difference between abstract and interface in PHP. Friends in need can come and refer to it. I hope it will be helpful to everyone.

1.php InterfaceClass: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? 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. For example:

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 InheritanceAll subclasses of this class must implement any of these three methods. 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, 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.'的商品');
}
}

Think about it, in a large project where many people cooperate, it is very convenient 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, indicating 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 this? In one sentence, abstract class extracts the part of the class image. This sentence looks funny, but in fact it tells the truth of abstract class. The function of abstract class is that when you find that many of your classes use many methods, you will continue to use them. If you are writing repeatedly, then you can consider using abstract classes. You may say, "Isn't it possible for me to rewrite a class? For each public class, I instantiate this public class and call the same method." Here Yes, in fact this is what the abstract class does, but it saves you the step of instantiation, making it as convenient as directly calling the method of this class, and you can alsooverload this way. 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);
     }
 }

Here is an example. Like the above, I defined a store class and extracted all its parts like buy and 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 code duplication, and improve reusability.

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 you need them. If If you feel dissatisfied if the service is not provided, you can still do it yourself. The abstract function method is somewhat similar to the interface. It is a method declared in the parent class and must be implemented in the subclass. However, methods not declared with abstract will become public methods of the subclass, and there is no need to implement them in the subclass

The above is the detailed content of Detailed explanation of the difference between PHP abstract and interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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