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.

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.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.