Home >Web Front-end >JS Tutorial >The difference between PHP abstract and interface_javascript skills

The difference between PHP abstract and interface_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:16:161320browse

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 may ask, how do I know how to name the implementation method of a certain function? Yes, 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:

Copy code 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 , look (view), then all subclasses that inherit this class must implement any of these three methods. If the subclass does not implement these, it will not work. 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 like:
Copy code The code is as follows:

class BaseShop implements Shop
{
public function buy($gid)
{
echo('You bought the product with the 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 the ID:'.$gid.');
}
}

Think about it, in a large project where multiple people collaborate , how convenient it is 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 this, I can't help it.

Conclusion: The interface class is the leader of a class, pointing out the direction. Subclasses must complete its designated methods.

2.php abstract class: abstract

In fact, abstract classes and interface classes are partially similar. I remember seeing this sentence somewhere, abstract classes extract the part of the class image. This sentence looks funny, but in fact it tells the truth of abstract classes. The role of abstract classes is that when you find that many of your classes use many methods that you keep rewriting, then you can consider using abstract classes. You may say, "I can't rewrite one class for every public class." I just instantiate this public class and call the same method." This is OK. In fact, this is what the abstract class does, but it saves you the step of instantiation, allowing you to directly It is just as convenient to call this class method, and you can also overload this method. Such as:

Copy code The code is as follows:

abstract class BaseShop
{
public function buy($gid)
                                                                                                                     {
echo ('The product you sold:'. $ Gid. '); 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.

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 you feel that you don’t If you are not satisfied with the service 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. There is no need to implement them in the subclass

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