Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Final class in PHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 108
    Comment on it

    Final class in PHP is a class that can not be inherited. or we can say that it can not be extended . It basically protect the methods of class that is to be overriden by the other child classes present there.

    For declaring a class as final, we have to prefix the class with 'final' keyword .

    Example :

    final class baseclass
                {
                    public function mymethod()  {
                              echo  "baseclass method";
                                }
    
                }
    
    
                class derivedclass extends baseclass
                 {
                            public function mymethod() {
    
                                     echo   "derivedclass method";
    
                                  }
    
                   }
    
    
              $c= new derivedclass();
              $c->mymethod();
    

    Given example have a base class which is declared as final .So it can not be inherited.if in any case any of derived class tries to extending this define baseclass then you will get the compile error as output.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: