Php object-oriented? Is Final Php object-oriented? Final class
Class. only instantiated objects cannot be used for inheritance.
When designing, this class can no longer be extended, so we should use the final syntax to limit it. other users should extend this class.
Definition:
Add the final keyword before the class.
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;
}
}
$ Book1 = new GoodsBook ('php', 234,56, 45 );
Another use of the Final keyword is used to limit the method:
This method cannot be overwritten when its 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;
}
// The output prices of all commodities should be consistent
Final public function sayPrice () // inherits this class. this method cannot be overwritten.
{
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";
}
}
$ Book1 = new GoodsBook ('php', 234,56, 45 );