In-depth analysis of PHPfinal keyword usage skills. PHP programmers have mastered that class inheritance in PHP is the most commonly used programming feature. we can create a base class (parent class) and then define some basic methods in it, in my PHP programmers
In PHP, class inheritance is the most commonly used programming feature. we can create a base class (parent class) and then define some basic methods in it, in our subclass, we can extend the methods in the parent class. this is called overwrite. However, if you have some key methods in the parent class, you cannot inherit from the quilt class, if the subclass inherits this method, it may cause some trouble for the program. Therefore, we hope this method is "private" and cannot be extended, in PHP, the final keyword is used to modify the methods you want to inherit.
PHP final keywords can modify classes and methods in classes, but they have similar functions, that is, if you use final keywords to modify them, the modified class or method cannot be extended or inherited. You can only honestly reference it. If you use final before the class, this indicates that this class cannot use inheritance. if you use the PHP final keyword before the method, this indicates that this method cannot be overwritten. The truth is that simple. let's take a 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 added the PHP final keyword before the class and the method to modify our specific class. of course, this class cannot be output as required. you can debug it by yourself, this is the basic principle. we only need to remember that if the class or method uses final, this means that the class cannot be inherited or the subclass cannot override the method of the parent class.
Class inheritance in PHP is the most used programming feature. we can create a base class (parent class) and then define some basic methods in this class. in my...