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

The difference between php abstract classes and interfaces, _PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:55:17
Original
792 people have browsed it

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 of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template