PHP interface class and abstract class

Source: Internet
Author: User

1. 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: CopyCode The Code is as follows: 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, and 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: copy Code the code is as follows: Class baseshop implements shop
{< br> Public Function buy ($ GID)
{< br> echo ('the ID you purchased is :'. $ GID. 'item ');
}< br> Public Function compute ($ GID)
{< br> echo ('your sold ID is :'. $ GID. 'item ');
}< br> Public Function view ($ GID)
{< br> echo ('you have viewed the ID :'. $ GID. 'item ');
}< BR >}

you can think about how convenient it is to have an interface class in a large project with multiple partners, so you don't have to ask others, what is the method name of your XX function? Of course, if you like this, I cannot.
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, an abstract class is similar to an interface class. Remember to extract the class image part from the abstract class when you see such a sentence, this sentence looks funny. In fact, it tells the truth of the abstract class. The role of the abstract class is that when you find that many of your classes are being repeatedly written in many ways, then you can consider using abstract classes. You may say, "I am not allowed to rewrite a class. Each public class will instantiate a public class and call the same method ", here we can, in fact, the work of the abstract class is also this, But it saves the step of your instantiation, making it as convenient as directly calling this class method, you can also reload this method. For example, the copy Code code is as follows: abstract class baseshop
{< br> Public Function buy ($ GID)
{< br> echo ('the ID you purchased is :'. $ GID. 'item ');
}< br> Public Function compute ($ GID)
{< br> echo ('your sold ID is :'. $ GID. 'item ');
}< br> Public Function view ($ GID)
{< br> echo ('you have viewed the ID :'. $ GID. 'item ');
}< BR >}< br> class ballshop extends baseshop
{< br> var $ itme_id = NULL;
public function _ construct ()
{< br> $ this-> itme_id = 2314;
}< br> Public Function open ()
{< br> $ this-> require ($ this-> itme_id);
}< BR >}

here is an example. I want to define a shop class like above, extract all its image parts, buy (buy), sell (sale ), view (view), and the abstract class implements these methods, then the child class that inherits it automatically obtains these methods, and the Child class makes its own unique things and introduces code duplication, improves reusability.
conclusion: an abstract class is a class service provider. It has many services and you do not need to use them. You can use them as needed, if you are not satisfied with the service, you can do it yourself.
well, the above is my opinion on the PHP interface class and abstract class. I hope I can help these two friends. If you have any comments, please leave a message!

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.