PHP Object-oriented? Final class
The class, which can only be instantiated by an object, cannot be used for inheritance.
At design time, the class can no longer be extended, it should be restricted by the final syntax, and other users should extend the class.
Defined:
Before class, add the final keyword.
Example:
Class Goods
{
Public $goods _name;
Public $shop _price;
Public function __construct ($name, $price)
{
$this->goods_name= $name;
$this->shop_price= $price;
}
}
Final class Goodsbook extends Goods
{
Public $pages;
Public function __construct ($name, $price, $pages)
{
Parent::__construct ($name, $price);
$this->pages= $pages;
}
}
$book 1 = new Goodsbook (' php ', 234,56,45);
Another usage of the Final keyword for restricting methods:
Restricts the method, which cannot be overridden when the owning class is inherited.
Example:
Class Goods
{
Public $goods _name;
Public $shop _price;
Public function __construct ($name, $price)
{
$this->goods_name= $name;
$this->shop_price= $price;
}
Public Function Sayname ()
{
Echo $this->goods_name;
}
All goods should be priced in the same way
Final public Function Sayprice ()//Inherit the class, the method cannot be overridden
{
Echo ' ¥ ', $this->shop_price;
}
}
Final class Goodsbook extends Goods
{
Public $pages;
Public function __construct ($name, $price, $pages)
{
Parent::__construct ($name, $price);
$this->pages= $pages;
}
Public Function Sayname ()
{
echo "$this->goods_name";
}
}
$book 1 = new Goodsbook (' php ', 234,56,45);