PHP abstract methods and abstract classes abstract keyword
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 one method in a class is an abstract method, then this 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:
abstract class AbstractClass{
// Define abstract method
abstract protected function getValue();
// Ordinary method
public function printOut(){
print $ this->getValue()."
";
}
}
class ConcreteClass extends AbstractClass{
protected function getValue(){
using using using using ’ through off Through off ‐ off ‐ ‐ return ‐ ‐ ‐ } }
}
class ConcreteClass extends AbstractClass{
}
$class1 = new ConcreteClass;
$class1->printOut();
?>