Home >Backend Development >PHP Tutorial >PHP abstract method and abstract class abstract keyword introduction and application_PHP tutorial

PHP abstract method and abstract class abstract keyword introduction and application_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:50762browse

Introduction and application of PHP abstract methods and abstract classes abstract keyword

PHP abstract methods and abstract classes abstract keyword

The 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 there is an abstract method in a class, then the 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:

The code is as follows:


abstract class AbstractClass{
// Define abstract method
abstract protected function getValue();
// Normal method
public function printOut(){
print $this->getValue()."
";
}
}
class ConcreteClass extends AbstractClass{
protected function getValue(){
return "Implementation of abstract method";
}
}

$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 child class.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/896771.htmlTechArticlePHP Introduction and application of abstract methods and abstract classes abstract keyword PHP abstract methods and abstract classes abstract keyword abstract keyword Used to define abstract methods and abstract classes. Abstract method extraction...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn