Home  >  Article  >  Backend Development  >  What is the difference between abstract class and interface in php

What is the difference between abstract class and interface in php

王林
王林Original
2020-11-02 13:50:036175browse

The difference between abstract classes and interfaces in php: 1. Abstract classes can have attributes and ordinary methods, while interfaces cannot have attributes and ordinary methods; 2. Abstract classes may not have abstract methods, but interfaces must have abstraction. Method; 3. Abstract classes are declared using the abstract keyword, and interfaces are declared using the interface keyword.

What is the difference between abstract class and interface in php

Difference introduction:

(Recommended tutorial: php video tutorial)

Abstract class: It is based on a class, which is a class itself. It is just a special class that cannot be directly instantiated. Methods and attributes can be defined in the class. Similar to templates, after specification, subclasses can implement detailed functions.

Interface: Mainly based on method specifications, a bit like abstract methods in abstract classes, except that it is more independent than abstract methods. A class can be combined into a new class by combining multiple methods.

The similarities between abstract classes and interfaces:

1. They are both used to declare a certain thing, standardize names and parameters, and form modules without 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.

The difference between abstract classes and interfaces:

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

2. There may not be abstract methods in abstract classes, but there must be " Abstract" method

3. The syntax is different

4. The abstract class is declared before the class with the abstract keyword, and class is declared as a class. The interface is declared with interface, but it cannot Use class to declare it, because interfaces are not classes.

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. Just use commas to separate each interface.

Related recommendations: php training

The above is the detailed content of What is the difference between abstract class and interface in php. 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