Home > Database > Mysql Tutorial > body text

The actual role of php interface classes and abstract classes

黄舟
Release: 2016-12-14 15:05:02
Original
1197 people have browsed it

1.php interface class: interface
In fact, their function is very simple. When many people develop a project together, they may call some classes written by others. Then you will ask, how do I know a certain function of it? How is the implementation method named? 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 the code 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, 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 the code as follows:
class BaseShop implements Shop
{
public function buy($gid)
{
echo('You purchased 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 many people collaborate, it is so convenient to have an interface class, so that you don’t have to go Ask others, what is the method name of your certain function? 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, and the subclass must complete its specified method.
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 similar parts. This sentence looks funny, but in fact it says Understand 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 are constantly rewriting, then you can consider using abstract classes. You may say, "I can't rewrite it." For each public class of a class, I 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. , making it as convenient as calling the method of this class directly, and you can also overload this method. For example:

Copy the code as follows:
abstract class BaseShop
{
public function buy($gid)
{
echo('You purchased 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 item with the ID: '.$gid.'); :'.$gid.'s product');
}
}
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 items like part, buy, sell, 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 the duplication of code 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 needed. If you feel dissatisfied with not providing services, you can do it yourself.
Haha, the above are my humble opinions on PHP interface classes and abstract classes. I hope it can help my friends who are confused about these two. If you have any comments, please leave a message! For more related articles, please pay attention to the PHP Chinese website (www.php.cn )!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!