PHP Interface Class: interface
Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.
Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.
All methods defined in the interface must be public, which is the attribute of the interface.
To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.
Note:
When implementing multiple interfaces, the methods in the interface cannot have duplicate names.
Interfaces can also be inherited by using the extends operator.
Class to implement an interface, it must be in a way that is exactly the same as the method defined in the interface. Failure to do so can result in fatal errors.
Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces.
When we define an interface class, it is in the way that the following subclass must be implemented, such as
Interface Shop
{
Public function Buy ($gid);
Public function sell ($GID);
Public 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.
In fact, the interface class is plainly, is a class template, a class of provisions, if you belong to this category, you must follow my rules, less one can not, but specific how you do, I do not care, such as:
Class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo ' you purchased the ID for: '. $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 for: '. $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
PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.
When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private. The method must be called in the same way, i.e. the type and the required number of parameters must be the same. For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different.
The role of abstract classes is that when you find many of your classes in many ways you are constantly repeating, 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 OK, In fact, this is what the abstract class does, but he saves you from instantiating this step, making it as easy as calling the method directly, and you can overload the method. Such as:
Abstract class Baseshop
{
Public function Buy ($gid)
{
Echo ' you purchased the ID for: '. $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 for: '. $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);
}
}
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.
- If you are creating a model, this model will be used by some closely related objects, and you can use abstract classes. The interface is used if you want to create features that will be used by some unrelated objects.
- If the behavior must be inherited from multiple sources, the interface is used.
- If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.
- The interface implements the constraint and unification (naming) of the specific method, and the abstract class implements the code reuse, and the combination effect is better.
Understanding of PHP's interface classes and abstract classes