PHP Programmers are mastering the
The inheritance of classes in PHP is one of the most used programming features, and we can create a base class (the parent Class) and then define some basic methods in this, in our subclass we can extend the method in the parent class which is called overwrite, but if you say you are in some very critical way in the parent class you cannot inherit the quilt class, Because if the subclass inherits this method may cause some trouble to the program, we hope that this method is "private" and cannot be extended, which uses the final keyword in PHP to modify the method you are not inheriting.
The PHP final keyword can be decorated with classes that can also modify methods in a class, but they do so in the same way that if you use the final keyword to decorate, the modified class or method cannot be extended or inherited. You just have to quote it honestly. If you use final in front of the class, this means that the class cannot use inheritance; if you use the PHP final keyword before the method, this means that the method cannot be overwritten. The truth is that it's so simple, let's look at a simple example.
- < ? Php
- Final class BaseClass {
- Public Function test () {
- echo "Baseclass::test () Calledn";
- }
- Final public Function moretesting () {
- echo "baseclass::moretesting () Calledn";
- }
- }
- Class ChildClass extends BaseClass {
- Public Function moretesting () {
- echo "childclass::moretesting () Calledn";
- }
- }
- Results in Fatal error:cannot override final Method Baseclass::moretesting ()
- ?>
In this example, we put the PHP final keyword in front of the class and before the method to decorate our specific class, of course, this class is not output as required, you can do your own debugging, the basic principle is this, We just have to remember that if the class or method uses final then this means that the class cannot be inherited or the subclass cannot overwrite the parent class's methods.
http://www.bkjia.com/PHPjc/446025.html www.bkjia.com true http://www.bkjia.com/PHPjc/446025.html techarticle PHP Programmers have mastered that the inheritance of classes in PHP is one of the most used programming features, and we can create a base class (parent Class) and then define some basic methods in it, in my ...