Introduction to php interface classes and abstract classes
Source: Internet
Author: User
1. php interface class: interface & nbsp; in fact, their functions are very simple, when many people develop a project together, they may call some classes written by others, and you will ask, how do I know how to name a function implementation method? in this case, phpSy 1 and php interface class: interface
In fact, their role is very simple. when many people develop a project together, they may call some classes written by others, and you will ask, how do I know how the implementation method of a function is named? at this time, the php interface class plays a role. when we define an interface class, the method in it must be implemented by the following sub-classes, for example:
Php interface and abstract class Code
Interface Shop
{
Public function buy ($ gid );
Public function compute ($ gid );
Public function view ($ gid );
}
I declare a shop interface class and define three methods: buy, sell, view ), therefore, none of the three methods must be implemented for all subclasses that inherit this class. if the subclass does not implement these methods, it will not be able to run. Actually, an interface class is a class template and a class rule. if you belong to this class, you must follow my rules, but how do you do it? I don't care. it's your business, for example:
Php interface and abstract class Code
Class BaseShop implements Shop
{
Public function buy ($ gid)
{
Echo (you purchased the product ID:. $ gid );
}
Public function compute ($ gid)
{
Echo (you have sold the product ID:. $ gid );
}
Public function view ($ gid)
{
Echo (you have viewed the product with ID:. $ gid );
}
}
You can think about how convenient it is to have an interface class in a large project that has many people working together, so you don't have to ask others, what is the method name of your xx function, of course, if you like this, I can't.
Conclusion: an interface class is the leader of a class. it specifies the direction and the subclass must specify the method.
2. php abstract class: abstract
In fact, the abstract class is very similar to the interface class. I remember where I saw this sentence. the abstract class extracts the class image part. this sentence looks funny. In fact, it tells the truth of the abstract class, the role of an abstract class is that when you find that many of your classes are repeatedly written using many methods, you can consider using an abstract class, you may say, "I am not allowed to override a class, and each public class will instantiate a public class. you can call the same method, in fact, this is the work of the abstract class, but it saves you the step of instantiation, making it as convenient as directly calling this class method, and you can also reload this method. For example:
Php interface and abstract class Code
Abstract class BaseShop
{
Public function buy ($ gid)
{
Echo (you purchased the product ID:. $ gid );
}
Public function compute ($ gid)
{
Echo (you have sold the product ID:. $ gid );
}
Public function view ($ gid)
{
Echo (you have 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-> require ($ this-> itme_id );
}
}
Here is an example. I want to define a shop class like above, extract all its image parts, buy, sell, and view ), and abstract classes all implement these methods, so the child classes that inherit it will automatically obtain these methods, and the child classes will do their own unique things, introduce code duplication, and improve reusability.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.