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.