Home  >  Article  >  Backend Development  >  The difference between php abstract classes and interfaces, _PHP tutorial

The difference between php abstract classes and interfaces, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:17726browse

The difference between PHP abstract classes and interfaces,

1. The use of interfaces is through the keyword implements. The use of abstract classes is through the keyword extends. Of course, interfaces can also be inherited through the keyword extends.

2. Member variables (including class static variables) cannot be declared in the interface, but class constants can be declared. Various types of member variables can be declared in abstract classes to implement data encapsulation.


3. Interfaces do not have constructors, but abstract classes can have constructors.

4. Methods in interfaces are of public type by default, while methods in abstract classes can be modified with private, protected, or 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 that will be used by some closely related objects, you can use abstract classes. Use interfaces if you want to create functionality that will be adopted by a number of unrelated objects.
    • If behavior must be inherited from multiple sources, use interfaces.
    • If you know that all classes will share a common behavior implementation, use an abstract class and implement that behavior within it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/992391.htmlTechArticleThe difference between php abstract classes and interfaces, 1. The use of interfaces is through the keyword implements. The use of abstract classes is through the keyword extends. Of course, the interface can also be continued through the keyword extends...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn