What is an abstract class for PHP object-oriented? and the role of abstract classes

Source: Internet
Author: User
In this article we describe what an abstract class is and how to use it.

What is an abstract class?

Abstract classes cannot be instantiated, and the same method is not implemented, just providing a method declaration, not a specific implementation. Abstract classes can only be used as parent classes of other classes. Abstract classes are declared using the abstract keyword, with the syntax in the following format:

Abstract class abstractname{...}

Attention:

Abstract and normal classes are similar, with both member variables and member methods. But there is a difference. The class that contains the abstract method must itself be abstract. There is no method body for an abstract method, and his function can only be done in subclasses. Abstract methods are also decorated with the abstract keyword in the following format:

Abstract function abstractname ();

The abstract method is followed by a semicolon, not the curly brace "{}".

What is the role of abstract classes?

In our class, there are a lot of classes that will be repeatedly written, this time we can use the concept of abstract class, write a common class, after instantiation we can repeat to invoke. Abstract class is like a big warehouse, there are a lot of things, not require you to use in the warehouse, you can call it when you need it. If you're not satisfied with what's in the warehouse, you can also build one yourself.

Abstract methods and abstract classes are primarily used in complex class hierarchies, which need to ensure that each subclass contains and overloads some specific methods, which can also be implemented through interfaces.

Abstract class examples are explained:

<?phpheader ("Content-type:text/html;charset=utf-8"); abstract class Member{abstract function Vipmember ($name, $ level, $money);} Class Member1 extends Member{function vipmember ($name, $level, $money) {echo "Your membership name in topic.alibabacloud.com is:". $name; Echo ' <br/> ', echo ' Your membership level is: '. $level, Echo ' <br/> ', echo ' the money you earned from the article is: '. $money;}} Class Member2 extends Member{function vipmember ($name, $level, $money) {echo "Your membership name in topic.alibabacloud.com is:". $name; Echo ' <br/> ', echo ' Your membership level is: '. $level, Echo ' <br/> ', echo ' you want to extract '. $money. "Yuan";}} $member 1 = new Member1 (), $member 2 = new Member2 (); $member 1->vipmember (' White ', ' three ', ' 24.35 '); Echo ' <br/> '; Echo ' 

First create a member abstract class that contains an abstract method Vipmember. Generates two subclass Member1,member2 for an abstract class, implementing an abstract method in two subclasses, respectively. Finally, instantiate two objects, call the implementation of the abstract method, the final output.

Related Article

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.