PHP final class of object-oriented methods and final

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.

code show as below:

<?
// 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

code show as below:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 14



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.

code show as below:

<?
// 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

 code show as below:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 16

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.

code show as below:

<?
// 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

code show as below:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 14



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.

code show as below:

<?
// 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

 code show as below:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 16

Guess you like

Origin www.cnblogs.com/showcase/p/11606203.html