Final --- before a class method.
--- final class can not be inherited.
--- final methods can not be overridden.
final class can not be inherited.
If we do not want a class to be inherited, we use the final decorating this class. This class can not be inherited. For example, we set the Math class, involving mathematical calculations we have to do, there is no need to modify these algorithms, there is no need to be inherited, we set it to the final type.
<?
// declare a final class Math
final class Math {
public static $ PI = 3.14;
public function the __toString () {
return "What is the Math class.";
}
}
$ New new Math Math = ();
echo $ Math;
/ / class declaration SuperMath Math class inherits from
class SuperMath the extends Math {
}
// will perform error, final class can not be inherited.
?>
Result of the program
final methods can not be overridden
if you do not want to type in a method overridden by subclasses, we can set this method is the final method, only need to add the final qualifier before this method.
If this method is overridden by subclasses, an error will occur.
<?
// declare a final class Math
class Math {
public static $ PI = 3.14;
public function the __toString () {
return "What is the Math class.";
}
Public final function max ($ A, $ B) {
return $ A > A $ B $:? $ B;
}
}
// declare classes SuperMath Math class inherits from
class SuperMath the extends Math {
public Final function max ($ A, $ B) {}
}
// will perform error, final method can not be rewriting.
?>
Result of the program
Original: https://blog.csdn.net/qq_41037819/article/details/80715785
Final --- before a class method.
--- final class can not be inherited.
--- final methods can not be overridden.
final class can not be inherited.
If we do not want a class to be inherited, we use the final decorating this class. This class can not be inherited. For example, we set the Math class, involving mathematical calculations we have to do, there is no need to modify these algorithms, there is no need to be inherited, we set it to the final type.
<?
// declare a final class Math
final class Math {
public static $ PI = 3.14;
public function the __toString () {
return "What is the Math class.";
}
}
$ New new Math Math = ();
echo $ Math;
/ / class declaration SuperMath Math class inherits from
class SuperMath the extends Math {
}
// will perform error, final class can not be inherited.
?>
Result of the program
final methods can not be overridden
if you do not want to type in a method overridden by subclasses, we can set this method is the final method, only need to add the final qualifier before this method.
If this method is overridden by subclasses, an error will occur.
<?
// declare a final class Math
class Math {
public static $ PI = 3.14;
public function the __toString () {
return "What is the Math class.";
}
Public final function max ($ A, $ B) {
return $ A > A $ B $:? $ B;
}
}
// declare classes SuperMath Math class inherits from
class SuperMath the extends Math {
public Final function max ($ A, $ B) {}
}
// will perform error, final method can not be rewriting.
?>
Result of the program