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

The difference between php abstract classes and interfaces

WBOY
Release: 2016-08-08 09:25:19
Original
1019 people have browsed it

1. The use of the interface 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, while 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, 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 that will be adopted 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.

The above has introduced the difference between PHP abstract classes and interfaces, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template