Final Methods in PHP


Final Methods:


The final keyword, which prevents child classesfrom overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. Note: Properties cannot be declaredfinal, only classes and methods may be declared as final.

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.


Key Points :

  1. final class cannot be extended.
  2. Final method can not be override by the child class.
  3. Class variables cannot be declare as final.

Program:

<!DOCTYPE html>

<html>

<head>   </head>

   <body>

      <p>PHP final classes and methods</p>

               <?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 "<p>this is bmw class </p>";}

final public function  price(){echo  "<p>bmw class final price method</p>";}

             }

class  Tata   extends   Bmw{

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

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

above method will throw a fatal error, because you cannot override final method

Bmw::price() */

                               }          

                                $bmw  =  new  Bmw();

                                $tata =   new  Tata();

                                $bmw->display();

                                $bmw->price(); // call the final method price()                      

                                $tata->display();

                                $tata->price() // call the final method price()

  ?>

   </body></html>

Output:

PHP final classes and methods

this is bmw class

bmw class final price method

this is tata class

bmw class final price method


PHP scope resolution operator is used for access final methods:

<!DOCTYPE html> 

<html>

   <head>   </head>

     <body>          

  <?php 

    final   class     Audi{      

                public   static   $country  = "VANSH GUPTA" ;                

                           const    PRODUCT_ID = "624";

                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                                            

  ?>

   </body></html>


Output:

VANSHGUPTA
624
audi static property = VANSH GUPTA

Custom Search

%d bloggers like this: