The difference between PHP abstract and interface

Source: Internet
Author: User
1.php interface class: interface actually their role is very simple, when there are a lot of people to develop a project, may go to call someone else to write some of the classes, then you will ask, how do I know how the implementation of one of his functions is named, this time the PHP interface class will play a role, When we define an interface class, it is in the way that the following subclass must be implemented, such as:
1 Interface Shop
2 {
3 Public Function Buy ($gid);
4 Public Function sell ($GID);
5 Public Function view ($GID);
6} I declare a shop interface class, define three methods: Buy, Sell (Sell), see (view), then inherit all subclasses of this class must implement these 3 methods less one, if the subclass does not implement these words, it will not run. In fact, the interface class is plainly, is a class template, a class of provisions, if you belong to this category, you have to follow my rules, less one can not, but specific how you do, I don't care, it is your business, such as:
Class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo (' You have purchased the ID: '. $gid. ' goods ');
}
Public function Sell ($gid)
{
Echo (' You have sold the ID: '. $gid. ' goods ');
}
Public Function view ($gid)
{
Echo (' You have viewed the item ID: '. $gid. ');
}
You think, in a multi-person cooperation of large projects, with the interface class is how convenient, so you do not have to ask others, your method name of the function is what, of course, if you like this I have no way. Conclusion: The interface class is the leader of a class, indicating the direction in which the subclass must complete its specified method. 2.php abstract class: Abstract In fact, there is a part of abstraction class and interface class, remember where to see such a sentence, the abstract class like the part of the extraction, the sentence looks very funny, in fact, it said the abstract class of truth, the role of abstract class is, When you find many of your classes in a lot of ways you repeatedly write, then you can consider the use of abstract classes, you may say "I can not rewrite a class each public class I instantiate a public class, call the same method can be", here is can, in fact, the work of abstract class is this, But he omitted this step of your instantiation, making it as convenient as calling the method directly, and you can overload the method. Such as:
1 abstract class Baseshop
2 {
3 Public Function Buy ($gid)
4 {
5 echo (' You have purchased an ID of: '. $gid. ' goods ');
6}
7 Public Function sell ($gid)
8 {
9 Echo (' You have sold the ID for: '. $gid. ' goods ');
10}
Public Function view ($gid)
12 {
Echo (' You have checked the ID for: '. $gid. ' goods ');
14}
15}
Class Ballshop extends Baseshop
17 {
var $itme _id = null;
Public Function __construct ()
20 {
$this->itme_id = 2314;
22}
Public Function open ()
24 {
$this->sell ($this->itme_id);
26}
27}
28 Here is an example, like the above I define a store class, draw out all of its like, buy, sell (Sell), look (view), and the abstract class has implemented these methods, then inherit its subclasses automatically obtained these methods, subclasses do its own unique things, Introduce the duplication of code and improve reusability. Conclusion: Abstract class is a kind of service provider, has a lot of services, you do not have to use, when necessary when you use it can, if you feel not to provide service dissatisfaction, you can also do it yourself. The abstract function method and interface are somewhat similar, that is, the method of declaring in the parent class, which must be implemented in the subclass, but the method that is not declared by the abstract will become the public method of the subclass, and it is not necessary to implement in the subclass



  • Related Article

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.