Home >Backend Development >PHP Tutorial >PHP abstract method and abstract class abstract keyword introduction and application_PHP tutorial
PHP abstract methods and abstract classes abstract keyword
The abstract keyword is used to define abstract methods and abstract classes.
Abstract method
Abstract method refers to a method without a method body. Specifically, when the method is declared, there is no {} brackets and its contents. Instead, it is directly declared with a semicolon after the method name.
abstract keyword is used to define abstract methods, syntax:
abstract function function_name();
Abstract class
As long as there is an abstract method in a class, then the class must be defined as an abstract class. Abstract classes are also defined using the abstract keyword.
Abstract classes cannot produce instance objects. Abstract methods are usually used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented. In fact, abstract classes are introduced to facilitate inheritance.
Example:
The code is as follows:
In this example, the parent class defines the abstract method and the implementation of the method, but the actual content is defined in the child class.