Php object-oriented? Final Class-php Tutorial

Source: Internet
Author: User
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 );

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.