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