Home > Article > Backend Development > In-depth interpretation of the problem analysis of abstract classes and abstract methods in PHP_PHP Tutorial
In object-oriented (OOP) languages, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to access. Abstract methods are introduced to facilitate inheritance. Now let’s take a look at how abstract classes and abstract methods are defined and their characteristics.
What is an abstract method? The method we define in the class with only the method name and no method body is an abstract method. The so-called no method body means that there is no curly braces and the content in the method when it is declared. Instead, when it is declared directly, a semicolon is added after the method name. , In addition, when declaring an abstract method, add a keyword "abstract" to modify it.
1. Abstract keyword: abstract
Abstract means that it cannot be explained exactly, but it has a certain concept or name. To declare an abstract class or method in PHP, we need to use the abstract keyword.
2. Definition of abstract methods and abstract classes
At least one method in a class is abstract, we call it an abstract class. So if you define an abstract class, first define the abstract method.
......
}
3. Rules for using abstract classes and methods
Several characteristics of abstract classes:
2. In inherited derived classes, all abstract methods must be overloaded before they can be instantiated
The statement about abstract methods is as follows:
What is an abstract class? As long as a method in a class is an abstract method, then the class must be defined as an abstract class. Abstract classes must also be modified with the keyword "abstract". Abstract classes cannot instantiate objects, so abstract methods are used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented.
protected function getUser(){
return "Abstract class and abstract method www.jb51.net";
}
}
$user->print_content(); //Abstract class and abstract method
?>
In PHP5.1, static abstract methods are supported in abstract classes. In the example below, we see that static abstract methods can be declared. When implementing this method, it must be a static method.