Abstract method refers to a method without a method body. As long as a method in a class is an abstract method, this class must be defined as an abstract class. If you do not know it, you can check it out.
Abstract method refers to a method without a method body. As long as a method in a class is an abstract method, this class must be defined as an abstract class. If you do not know it, you can check it out.
PHP abstract methods and abstract keywords
Abstract keywords are used to define abstract methods and abstract classes.
Abstract Method
An abstract method is a method without a method body. Specifically, when a method is declared, there is no {} arc or content in it. Instead, the method name is directly added with a semicolon after the method name.
Abstract keywords are used to define abstract methods. Syntax:
Abstract function function_name ();
Abstract class
As long as a method in a class is an abstract method, this class must be defined as an abstract class. Abstract classes are also defined using abstract keywords.
Abstract classes cannot generate instance objects. They are usually used as templates that overload subclass methods, and all methods in the inherited abstract classes must be implemented. Abstract classes are introduced to facilitate inheritance.
Example:
The Code is as follows:
<? Php
Abstract class AbstractClass {
// Define the abstract Method
Abstract protected function getValue ();
// Common method
Public function printOut (){
Print $ this-> getValue ()."
";
}
}
Class ConcreteClass extends AbstractClass {
Protected function getValue (){
Return "Implementation of abstract methods ";
}
}
$ Class1 = new ConcreteClass;
$ Class1-> printOut ();
?>
In this example, the parent class defines the abstract method and the implementation of the method, but the actual content is defined in the subclass.