Php Object-oriented Abstract class

Source: Internet
Author: User
Php Object-oriented Abstract Class Php object-oriented -- Abstract class

Abstract class abstract:

There is a type that can only be inherited and cannot be instantiated. The reason is that the definition of this class is incomplete.

Because php supports defining only the declaration part of the method, but not the incomplete method of the implementation part of the method.

If a class contains this incomplete method, it is not an incomplete class and the object cannot be instantiated.

Incomplete class, called abstract class

The incomplete methods are called abstract methods.

Definition:

Classes that contain abstract methods are abstract classes.

Syntax:

Define the abstract method and use the abstract keyword to inform php that the method is an abstract method.

If a class contains an abstract method, that is, an abstract class, you also need to use the abstract keyword to declare

Example:

Abstract class Goods

{

Public $ goods_name;

Public $ shop_price;

Public function _ construct ($ name, $ price)

{

$ This-> goods_name = $ name;

$ This-> shop_price = $ price;

}

// Abstract method

Abstract public function sayName ();

}

Class GoodsBook extends Goods

{

Public $ pages;

Public function _ construct ($ name, $ price, $ pages)

{

Parent: :__ construct ($ name, $ price );

$ This-> pages = $ pages;

}

// Inherit the subclass of the abstract class to implement the abstract method in the abstract class

Public function sayName ()

{

Echo $ this-> goods_name;

}

}

Abstract classes do not have the ability to instantiate objects. they only have the ability to be inherited.

If the class that inherits an abstract class is not an abstract class, you must implement incomplete abstract methods. Otherwise, the class should also be an abstract class.

Note: When the abstract method of an abstract class is implemented in a subclass, the method names and parameters must be consistent, and the access permission must be weaker than that of the abstract class.

Example:

Abstruct class Goods

{

Abstruct protected function sayName ();

}

Class GoodsBook extends Goods

{

Public function sayName ()

{

}

}

Abstract class Function Summary:

You can restrict the structure of methods used by subclasses while providing public operations for them.

It sacrifices the function of instantiating objects.

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.