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.