PHP interface and abstraction comprehension _ PHP Tutorial

Source: Internet
Author: User
Understanding of PHP interfaces and abstractions. Today, a younger brother asked me, what is the use of the interface class and abstract class in php? it is useless at all and doesn't mean anything, in fact, his idea is the same. Today, a younger brother asked me what is the use of the interface class and abstract class in php, and he will not use it at all, I think it is useless. In fact, it is the same as his idea. When I first came into contact with php, I also thought it was useless. later, I gradually came into contact with the big system, in fact, these things still have a certain effect. let me briefly talk about them.

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, such

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, 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:

Class BaseShop implements Shop {public function buy ($ gid) {echo 'you purchased the ID :'. $ gid. 'commodity ';} public function Consumer ($ gid) {echo' your purchase ID is :'. $ gid. 'item ';} public function view ($ gid) {echo' you viewed ID :'. $ gid. 'commodity ';}}

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.

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:

Abstract class BaseShop {public function buy ($ gid) {echo 'you purchased the ID :'. $ gid. 'commodity ';} public function Consumer ($ gid) {echo' your purchase ID is :'. $ gid. 'item ';} public function view ($ gid) {echo' you viewed ID :'. $ gid. 'commodity ';} 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 (buy), sell (Sale), view (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.

Conclusion: an abstract class is a service provider of a class. it has many services that you don't need to use. you can use it when you need it. if you don't feel satisfied with the service, you can also do it yourself.

The above is my opinion on php interface classes and abstract classes, hoping to help these two friends.

...

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.