Today, a little brother asked me, PHP inside the interface class, the abstract class exactly what is the use of it, he will not use, feel that with no meaning, in fact, and his idea is the same, in my contact with PHP, but also feel that this thing is no use, in the subsequent gradual contact with the big system, Only to find that these things still have a certain role, the following I will simply say.
PHP Interface Class: interface
In fact, their role is very simple, when a lot of people to develop a project, it may be 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, The way inside it is that the following subclass must be implemented, such as
Interface shop{public function Buy ($gid);p ublic function Sell ($gid);p ublic function View ($gid);}
I declare a shop interface class, defines three methods: Buy, Sell (Sell), see (view), then inherit all subclasses of this class must implement these 3 methods less than 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 purchased the ID: '. $gid. ' The goods ';} Public function Sell ($gid) {echo ' Your purchase ID is: '. $gid. ' The goods ';} Public Function View ($gid) {echo ' You have browsed the ID: '. $gid. ' The Goods ';}}
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.
PHP Abstraction class: Abstract
In fact, the abstract class and interface classes are very similar, remember where to see such a sentence, the abstract class like the part of the extraction, this looks very funny, in fact, it said the abstract class of truth, the role of abstract classes, when you find many of your classes in a lot of ways you constantly in the repeated writing, Then you can consider using the abstract class, you might say "I can not rewrite a class each public class I instantiate a public class, call the same method can be," Here is OK, actually the abstract class does the work of this, but he omitted you instantiate this step, It's as handy as calling this method directly, and you can overload the method. Such as:
Abstract class Baseshop {public function buy ($gid) {echo ' you purchased the ID: '. $gid. ' The goods ';} Public function Sell ($gid) {echo ' Your purchase ID is: '. $gid. ' The goods ';} Public Function View ($gid) {echo ' You have browsed the ID: '. $gid. ' The Goods ';}} 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 define a store class, pumping all its like parts, buying (Buy), selling (Sell), looking (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 above is my PHP interface class, abstract class some humble opinion, I hope I can not understand the two friends some help.
http://www.bkjia.com/PHPjc/752505.html www.bkjia.com true http://www.bkjia.com/PHPjc/752505.html techarticle today, a little brother asked me, PHP inside the interface class, the abstract class what is the use of it, he will not use, feel that use also no meaning, in fact, and his idea is the same, in my ...