PHP abstract methods and abstract classes abstract keyword

巴扎黑
Release: 2016-11-11 17:55:05
Original
1444 people have browsed it


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();

?>

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. 🎜🎜🎜🎜
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template