Final keyword +const keyword

Source: Internet
Author: User

Final keyword

1. If we want a class to not be inherited by other classes (perhaps because of security considerations), you can use final.

Examples

<?    Final class a{}     class extends a{}; // will error    Echo "OK";? >

2. If we want a method, such as a method for calculating personal income tax, that cannot be overridden by a subclass, you can use final to modify the method.

Examples

<?PHPclassa{Final  Public functionGetrate ($salary){    return $salary*0.08;}} classBextendsa{//Unable to overwrite the Getrate method of the parent class    /*Public function Getrate ($sal) {return $sal *0.01;} */}    $b=NewB (); Echo $b->getrate (100);?>


The 3.final keyword cannot be modified to a property.

const keyword

The basic usage is:

Class a{

Const constant NAME = assigned initial value;

}

Interface interface Name {

Const constant NAME = assigned initial value;

}

Note: Constants are public

Examples

<? PHP     class a{    const tax_tate=0.08;      Public function paytax ($va 1) {    return$va 1*a::tax_tate;}}     $a=new  A ();     Echo $a->pay tax (+);? >

Final keyword +const keyword

Related Article

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.