Introduction and application of PHP abstract method and abstract-class abstract keyword

Source: Internet
Author: User
Tags abstract

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.


The code is as follows:
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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.