The difference between PHP abstract and interface

  • 2020-03-27 00:09:39
  • OfStack

1. PHP interface class :interface

Their role is very simple, actually when there are a lot of people together to develop a project, may go to call others to write some of the classes, then you will ask, how do I know he's a function of the method is how named, PHP interface class to play a role, this time when we define an interface class, it is the following subclasses must implement, such as:


 interface Shop
 {
       public function buy($gid);
       public function sell($gid);
       public function view($gid);
 }

If I declare a shop interface class that defines three methods: buy, sell, and view, then all subclasses that inherit this class must implement none of the three methods. In fact, the interface class is simply the template of a class and the rules of a class. If you belong to this class, you must follow my rules. None of them will work.

class BaseShop implements Shop
{
public function buy($gid)
{
echo(' You have purchased ID for  :'.$gid.' The goods ');
}
public function sell($gid)
{
echo(' You sell ID for  :'.$gid.' The goods ');
}
public function view($gid)
{
echo(' You looked at the ID for  :'.$gid.' The goods ');
}
}

Think about how convenient it is to have an interface class in a large collaborative project, so you don't have to ask people what the method name of your x function is, and I can't help it if you like.

Conclusion: an interface class is the leader of a class, pointing the way, and subclasses must complete it to specify methods.

PHP abstract class: abstract

Is an abstract class and interface class some like, remember where I saw it in a word, an abstract class is the class as part of the draw out, it looks very funny, it spoke the truth of the abstract class, the role of abstract classes is that when you found that many of your class written in many ways you constantly repeated, you can consider to use abstract classes, you may say "I'm not every public class can override a class I instantiate an it is a public class, it is ok to call the same method", this is can, in fact an abstract class work is also this, but he saves you instantiate this step, It allows you to call the method as if it were directly from this class, and you can also override the method. Such as:


  abstract class BaseShop
  {
      public function buy($gid)
      {
          echo(' You have purchased ID for  :'.$gid.' The goods ');
      }
      public function sell($gid)
      {
          echo(' You sell ID for  :'.$gid.' The goods ');
     }
     public function view($gid)
     {
         echo(' You looked at the ID for  :'.$gid.' The goods ');
     }
 }
 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 to the above as I class defines a shop, take it all as part of the buy (buy), sell (sell), the (view), and implements these methods in the abstract class, then inherit its subclasses are automatically obtained these methods, subclasses can do its own unique, introducing the repetition code, enhance reusability.

Conclusion: Abstract class is a class of service providers, there are many services, you do not have to use, when you need to use it, if you do not feel satisfied with the service, you can do it yourself. The abstract function method is similar to interface in that it is a declared method in the parent class, which must be implemented in the subclass. However, the method without the abstract declaration will become the public method of the subclass, which is not necessary to be implemented in the subclass


Related articles: