In-depth analysis of PHP final keyword usage tips _php tutorial

Source: Internet
Author: User
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.

 
 
    1. < ? Php
    2. Final class BaseClass {
    3. Public Function test () {
    4. echo "Baseclass::test () Calledn";
    5. }
    6. Final public Function moretesting () {
    7. echo "baseclass::moretesting () Calledn";
    8. }
    9. }
    10. Class ChildClass extends BaseClass {
    11. Public Function moretesting () {
    12. echo "childclass::moretesting () Calledn";
    13. }
    14. }
    15. Results in Fatal error:cannot override final Method Baseclass::moretesting ()
    16. ?>

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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.