Introduction and Application of abstract keywords in PHP abstract methods and abstract classes

Source: Internet
Author: User
Tags define abstract
Abstract method refers to a method without a method body. As long as a method in a class is an abstract method, this class must be defined as an abstract class. If you do not know it, you can check it out.

Abstract method refers to a method without a method body. As long as a method in a class is an abstract method, this class must be defined as an abstract class. If you do not know it, you can check it out.

PHP abstract methods and abstract keywords
Abstract keywords are used to define abstract methods and abstract classes.

Abstract Method

An abstract method is a method without a method body. Specifically, when a method is declared, there is no {} arc or content in it. Instead, the method name is directly added with a semicolon after the method name.

Abstract keywords are used to define abstract methods. Syntax:
Abstract function function_name ();

Abstract class

As long as a method in a class is an abstract method, this class must be defined as an abstract class. Abstract classes are also defined using abstract keywords.
Abstract classes cannot generate instance objects. They are usually used as templates that overload subclass methods, and all methods in the inherited abstract classes must be implemented. Abstract classes are introduced to facilitate inheritance.

Example:

The Code is as follows:


<? Php
Abstract class AbstractClass {
// Define the abstract Method
Abstract protected function getValue ();
// Common method
Public function printOut (){
Print $ this-> getValue ()."
";
}
}
Class ConcreteClass extends AbstractClass {
Protected function getValue (){
Return "Implementation of abstract methods ";
}
}

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

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.