1.php Interface Class: interface
In fact, their role is very simple, when a lot of people together to develop a project, may go to call someone else to write some of the class, then you will ask, how do I know how a certain function of his implementation method is named, this time the PHP interface class has played a role, when we define an interface class, The way inside it is that the following subclasses must be implemented, such as:
Copy Code code as follows:
Interface Shop
{
Public function ($gid);
Public function sell ($GID);
Public Function view ($GID);
}
I declare a shop interface class, defined three methods: Buy (purchase), sell (Sell), see (view), then inherit this class all subclasses must implement these 3 methods less than one, if the subclass does not implement these words, can not run. In fact, the interface class plainly, is a class template, a class of provisions, if you belong to this category, you must follow my rules, less than one, but the specific how you do, I do not care, it is your business, such as:
Copy Code code as follows:
Class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo (' you purchased the product with ID: '. $gid. ');
}
Public function Sell ($gid)
{
Echo (' You sold the product with ID: '. $gid. ');
}
Public Function view ($gid)
{
Echo (' You look at the product with ID: '. $gid. ');
}
}
You think, in a large number of people cooperation project inside, have the interface class is how convenient, so you do not have to ask others, your certain function of the method name is, 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, abstract classes and interface classes are a lot like, remember where to see such a word, abstract class to take out the part of the class, this sentence looks very funny, in fact it tells the truth of abstract class, the role of abstract class, when you find that many of your classes in a lot of ways you continue to write repeatedly, Then you can consider using an abstract class, you might say, "I'm not allowed to rewrite a class for every public class I instantiate a common class, call the same method," and here is what the abstract class does, but it saves you from instantiating this step, Makes you as handy as calling this class of methods directly, and you can overload this method. Such as:
Copy Code code as follows:
Abstract class Baseshop
{
Public function Buy ($gid)
{
Echo (' you purchased the product with ID: '. $gid. ');
}
Public function Sell ($gid)
{
Echo (' You sold the product with ID: '. $gid. ');
}
Public Function view ($gid)
{
Echo (' You look at the product with ID: '. $gid. ');
}
}
Class Ballshop extends Baseshop
{
var $itme _id = null;
Public Function __construct ()
{
$this->itme_id = 2314;
}
Public Function open ()
{
$this->sell ($this->itme_id);
}
}
Here's an example, like the above I've defined a store class, pulling out all the parts of it, buying (Buy), selling (Sell), looking (view), and implementing these methods in an abstract class, the subclass of inheriting it automatically obtains these methods, and subclasses do their own unique things, Introduce the duplication of code and improve the reusability.
Conclusion: Abstract class is a class of service providers, with many services, you do not have to use, when the need for you to use it, if you do not feel satisfied with the service, you can do it yourself.
Oh, the above is my PHP interface class, abstract class some humble opinion, I hope to be able to confuse these two friends some help, if what comments welcome message!