PHP Object-oriented abstract class

Source: Internet
Author: User
PHP Object-oriented-abstract class

Abstract class Abstraction:

There is a class that can only be inherited and cannot instantiate an object. The reason is that the definition of this class is incomplete.

Because PHP supports defining one, only the declaration part of the method, without 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.

An incomplete class, called an abstract class.

Contains incomplete methods, called abstract methods.

Defined:

The class that contains the abstract method is the abstract class.

Grammar:

Define abstract methods, using abstract keywords to tell PHP that method as an abstract method

If a class contains an abstract method, which is an abstract class, you also need to declare it using the abstract keyword

Example:

Abstract class Goods

{

Public $goods _name;

Public $shop _price;

Public function __construct ($name, $price)

{

$this->goods_name= $name;

$this->shop_price= $price;

}

Abstract methods

Abstract public Function sayname ();

}

Class Goodsbook extends Goods

{

Public $pages;

Public function __construct ($name, $price, $pages)

{

Parent::__construct ($name, $price);

$this->pages= $pages;

}

Inheriting the subclass of an abstract class to implement an abstract method in an abstract class

Public Function Sayname ()

{

echo$this->goods_name;

}

}

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

If the class inheriting an abstract class is non-abstract, it is necessary to implement an incomplete abstract method, otherwise the class should be an abstract class.

Note: When you implement an abstract method of an abstract class in a subclass, the method name, parameters are consistent, and access permissions are weaker than the access rights of the abstract class.

Example:

Abstruct class Goods

{

Abstruct protected function sayname ();

}

Class Goodsbook extends Goods

{

Public Function Sayname ()

{

}

}

Summary of abstract class functions:

Restricts the structure of the methods used by subclasses while providing common operations for subclasses.

The ability to instantiate an object is sacrificed.

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