Home >Backend Development >PHP Problem >What does php abstract class mean?

What does php abstract class mean?

WBOY
WBOYOriginal
2022-06-24 15:41:502091browse

PHP abstract class refers to a class with at least one abstract method; abstract classes cannot be instantiated, but only provide method declarations without specific implementations. Abstract classes can only be used as parent classes of other classes and can be used The abstract keyword is used to declare an abstract class, and the syntax is "abstract class AbstractName{...}".

What does php abstract class mean?

The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer

What does php abstract class mean

Specifically refers to relatively empty classes, generally used to define base classes, keyword abstract, abstract classes are not allowed to be instantiated, abstract classes must be inherited, abstract classes can have non-abstract methods, and a class with an abstract method, then The class must be an abstract class;

Abstract class: At least one method in a class is abstract, we call it an abstract class.

Requirements:

1. There must be at least one abstract method in a class

2. Abstract methods are not allowed to have {}

3.In front of abstract methods Must add abstract

4. Abstract classes are only allowed to be inherited and cannot be instantiated

Abstract classes cannot be instantiated, and the same method is not implemented. It only provides method declaration without specific implementation. Abstract classes can only be used as parent classes of other classes. Abstract classes are declared using the abstract keyword, and the syntax format is:

abstract class AbstractName{
.....
}

Note:

Abstract classes are similar to ordinary classes, both have member variables and member methods. But there is a difference. A class containing an abstract method must itself be abstract. An abstract method has no method body, and its functions can only be completed in subclasses. Abstract methods are also modified using the abstract keyword. The format is: linear motor parameters

abstract function abstractName();

The abstract method is followed by a semicolon instead of curly braces "{}".

What is the role of abstract classes?

Among our classes, many classes will be written repeatedly. At this time, we can use the concept of abstract class to write a public class, and we can call it repeatedly after instantiation. An abstract class is like a large warehouse with many things in it. You are not required to use everything in the warehouse. You can call it when you need it. If you feel dissatisfied with the items in the warehouse, you can also create a new one yourself.

Abstract methods and abstract classes are mainly used in complex class hierarchies. This hierarchical relationship needs to ensure that each subclass contains and overloads certain specific methods. This can also be achieved through interfaces.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does php abstract class mean?. 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