Abstract method refers to a method without a method body. As long as there is a method in a class that is an abstract method, then this class must be defined as Abstract class. Friends who don’t understand can take a look
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 are no {} brackets and the contents within it, but directly in the declaration. End 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 it is in one class If a method 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 generate instancesObjects, abstract methods are usually used as templates for subclassesMethod overloading, and all methods in the inherited abstract class must be accomplish. In fact, abstract classes are introduced to facilitate inheritance.
Example:
The code is as follows:
<?php abstract class AbstractClass{ // 定义抽象方法 abstract protected function getValue(); // 普通方法 public function printOut(){ print $this->getValue()."<br />"; } } class ConcreteClass extends AbstractClass{ protected function getValue(){ return "抽象方法的实现"; } } $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 subclasses.
The above is the detailed content of Detailed explanation of abstract keyword definition and usage examples in PHP. For more information, please follow other related articles on the PHP Chinese website!