PHP  Final classes - methods

a php class can declared final to indicate that it cannot be extended; that is, one cannot declare subclasses of a final class.a final class behavior cannot be changed by extending the class. a final class must be complete.

the class properties cannot be declared final, only non-abstract classes and methods may be declared as final.

syntax:

         final    class   audi{  }
         final    public   function  set_color(){}

some quick points :

  • a final class cannot be extended.
  • final method can not be override by the child class.
  • class variables cannot be declare as final.
   <?php                 
                  
            final   class   automobile{  //this class can not be extended            
                              
                                                 final    public  function   set_color(){   }
                                       
                                                 final    public  function   show(){  }                                       
                                       
                                            }
                                            
                                                 
  /*class audi extends automobile this line throw an error, because final class can't be extended.            
                                       {           
           
                                       }   */
                                       
 class  bmw{   
                 public  function   display(){ echo " this is bmw class " ; }

                 final  public  function   price(){  echo  "bmw price";  }  
                                             
              }

 class  tata   extends   bmw{


                 public   function  display(){   echo  "<br/>this is tata class"; }

    /* public   function  price(){  echo  "tata price" ; } 

  above method will throw a fatal error, because you cannot override final method bmw::price() */ 
              
      }
            
  ?>

we can easily access constants, static variables, public functions and final functions from final classes using scope resolution operator.

  <?php
  
   final   class     audi{
           
                 public   static   $country  = "usa" ;
                             
                            const    product_id = "34fk34";
                                      
                 public   $color  = "red" ;
                             
                 public   $price  = "$45343";
                             
    public   static   function  show(){  

                                     echo  "audi static property = ".audi::$country; }
                             
                                          
   public   final   function   id(){ 

                        echo "<br/> final method display audi id = ". audi::product_id; }
                        
                             
   public   function  get_data(){
                             
                                         echo  "<br/> audi color = red";
                                                                  
                                         echo  "<br/> audi price = $343434";
                                         
                                  
                         }
                                                              
            }                            
                                                                  
     echo     audi::$country."<br/>"; // this will work 
                 
     echo     audi::product_id. "<br/>";  // this will work                                  
                          
  audi::show(); //this will work
                 
   ?>