Home >Backend Development >PHP Problem >What is the difference between PHP interface and abstract class?

What is the difference between PHP interface and abstract class?

Guanhui
GuanhuiOriginal
2020-07-18 14:45:392463browse

What is the difference between PHP interface and abstract class?

What is the difference between PHP interface and abstract class?

1. Abstract classes can have attributes, ordinary methods, and abstract methods, but interfaces cannot have attributes, ordinary methods, and can have constants.

2. Abstract classes may not have abstract methods. , but there must be "abstract" methods in the interface

3. The syntax is different

4. The abstract class is declared before the class with the abstract keyword, and the class is declared as a class, interface It is declared with interface, but cannot be declared with class, because interface is not a class.

5. The abstract method of an abstract class must be declared with abstract, but the interface does not need to be

6. An abstract class uses the extends keyword to allow the subclass to inherit the parent class. Classes implement detailed abstract methods. The interface uses implements to allow ordinary classes to implement detailed methods of the interface in the class, and the interface can implement multiple methods at one time. Use commas to separate each interface.

PHP interface and abstract class The same points

1. They are all used to declare a certain thing, standardize the name and parameters, and form a module. There are no detailed implementation details.

2. Relevant detailed work is implemented through classes.

3. Grammatically, the abstract method of an abstract class is the same as an interface and cannot have a method body, that is, the {} symbol

4. Inheritance can be used. Interfaces can inherit interfaces to form new interfaces, and abstract classes can inherit abstract classes to form new abstract classes.

Recommended tutorial: "PHP



The above is the detailed content of What is the difference between PHP interface and abstract class?. For more information, please follow other related articles on the PHP Chinese website!

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