What are the differences between abstract classes and interfaces in php

王林
Release: 2023-03-07 11:20:01
Original
1775 people have browsed it

The differences between abstract classes and interfaces in php are: 1. Abstract classes can have attributes and common methods, while interfaces cannot have attributes and common methods; 2. Abstract classes do not necessarily have abstract methods, and interfaces do There are abstract methods; 3. The abstract methods of abstract classes must be declared abstract, but interfaces do not.

What are the differences between abstract classes and interfaces in php

The similarities between abstract classes and interfaces:

(Recommended video: java video tutorial)

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

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

Their respective characteristics:

There may not be abstract methods in an abstract class, but a class with abstract methods must be an abstract class

In an abstract class, even if it is a concrete method, it cannot be instantiated. You only need to create a new class to inherit it. Only after the instance can inherit the class

Interface allows a class to implement multiple different methods at one time

The interface itself is abstract, but note that it is not an abstract class, because the interface is not a class, just Its approach is abstract. Therefore, it is also abstract.

Related recommendations: php training

The above is the detailed content of What are the differences between abstract classes and interfaces in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!