PHP abstract method and abstract class Abstracts keyword
The abstract keyword is used to define abstraction methods and abstract classes.
Abstract methods
Abstract methods are methods that do not have a method body, specifically when the method declaration does not have {} brackets and the contents of it, but directly at the time of declaration by adding a semicolon to the end of the method name.
The abstract keyword is used to define an abstraction method, syntax:
Abstract function function_name ();
Abstract class
As long as one of the classes has an abstract method, the class is defined as an abstract class. Abstract classes are also defined with the abstract keyword.
Abstract classes cannot produce instance objects, often using abstract methods as templates for subclass method overloading, and to implement methods in inherited abstract classes. Abstract classes are actually introduced for easy inheritance.
Example:
The code is as follows:
<?php
Abstract class abstractclass{
Defining abstract methods
Abstract protected function getValue ();
Common method
Public Function PrintOut () {
Print $this->getvalue (). " <br/> ";
}
}
Class Concreteclass extends abstractclass{
protected function GetValue () {
Return "Implementation of abstract methods";
}
}
$class 1 = new Concreteclass;
$class 1->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.