final------------------------------
Final class---cannot be inherited.
Final method---cannot be overwritten.
The final class cannot be inherited.
If we do not want a class to be inherited, we use final to modify the class. This class will not be inherited. For example, the Math class we set involves the mathematical calculation methods we want to do. There is no need to modify these algorithms or inherit them. We set it to final type.
<?
//Declare a final class Math
final class Math{
public static $pi = 3.14;
public function __toString(){
return "This is the Math class.";
}
}
$math = new Math();
echo $math;
//Declaration class SuperMath inherits from Math class
class SuperMath extends Math {
}
//Execution will cause an error, and the final class cannot be inherited.
?>
Program running results
Final method cannot be rewritten
If we do not want a method in the class to be rewritten by a subclass, we can set this method to a final method, just add a final modifier before this method.
If this method is rewritten by a subclass, an error will occur.
<?
//Declare a final class Math
class Math{
public static $pi = 3.14;
public function __toString(){
return "This is the Math class.";
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
//Declaration class SuperMath inherits from Math class
class SuperMath extends Math {
public final function max($a,$b){}
}
//There will be an error in execution, and the final method cannot be rewritten.
?>
Program running results
Final class---cannot be inherited.
Final method---cannot be overwritten.
The final class cannot be inherited.
If we do not want a class to be inherited, we use final to modify the class. This class will not be inherited. For example, the Math class we set involves the mathematical calculation methods we want to do. There is no need to modify these algorithms or inherit them. We set it to final type.
Copy the codeThe code is as follows:
<?
//Declare a final class Math
final class Math{
public static $pi = 3.14;
public function __toString(){
return "This is the Math class.";
}
}
$math = new Math();
echo $math;
//Declaration class SuperMath inherits from Math class
class SuperMath extends Math {
}
//Execution will cause an error, and the final class cannot be inherited.
?>
Program running results
Copy the codeThe code is as follows:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\ on line 14
Final method cannot be rewritten
If we do not want a method in the class to be rewritten by a subclass, we can set this method to a final method, just add a final modifier before this method.
If this method is rewritten by a subclass, an error will occur.
Copy the codeThe code is as follows:
<?
//Declare a final class Math
class Math{
public static $pi = 3.14;
public function __toString(){
return "This is the Math class.";
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
//Declaration class SuperMath inherits from Math class
class SuperMath extends Math {
public final function max($a,$b){}
}
//There will be an error in execution, and the final method cannot be rewritten.
?>
Program running results
Copy the codeThe code is as follows:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\ on line 16