Differences between php abstract classes and interfaces,
1. The use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course, the interface can also be inherited by the keyword extends.
2. member variables (including static class variables) cannot be declared in the interface, but class constants can be declared. Abstract classes can declare various types of member variables to encapsulate data.
3. The interface has no constructor, and the abstract class can have constructor.
4. Methods in the interface are of the public type by default, while methods in the abstract class can be modified using private, protected, and public.
5. A class can implement multiple interfaces at the same time, but a class can only inherit from one abstract class.
Abstract class or interface.
- If you want to create a model, this model will be used by closely related objects, you can use abstract classes. If you want to create functions that will be used by irrelevant objects, use interfaces.
- If the behavior must be inherited from multiple sources, the interface is used.
- If you know that all classes share a public behavior implementation, use an abstract class and implement this behavior in it.