PHP method overriding

php method overriding : a super class and sub class both have methods with same name this is called method overriding.

instance methods can override instance methods and static methods can override static methods(method hiding).

while overriding a method the override method in the subclass can be less restrictive, but it cannot be more restrictive.

    class    ferrari{
    public static function ferrari_show(){ 
                        echo "ferrari class static show method <br/>";}
    public     function    look(){  echo  "ferrari class look method <br/>" ; }
 class    ferrari_model   extends  ferrari{         
    public  static   function ferrari_model_show(){  
                         echo "ferrari_model class static show method <br/>";}   
    public   function   look(){  echo   "ferrari_model class look method <br/>";}
/* private  function  look(){}   
  protected function look(){}   both commented method statement will throw an error,   
                                   because  override method in the subclass cannot more restrictive*/    
                   $ferrari_m   =  new  ferrari_model();
                   ferrari_model::ferrari_model_show(); // call sub class show() method
                   $ferrari_m->look(); // call sub class look() method
                   ferrari::ferrari_show();  // call super class ferrari_show() method
                   $fe = new  ferrari();
                   $fe->look();  // call super class look() method

you can call the parent class's overridden method from the child class's method by using the parent keyword look the below example:


 class    top{
 public static function top_show(){echo "top class static top_show method <br/>";}
 public     function   top_look(){echo  "top class top_look method <br/>" ; }
 class    bottom   extends  top{         
    static   function  top_show(){ 
                                 parent::top_show();// call the super class top_show method 
                                 echo "bottom class static bottom_show method <br/>";   }   
    function   top_look(){  
               parent::top_look(); // call the super class top_look() method
               echo   "bottom class bottom_look method <br/>";   }                  
                   $bottom_obj   =  new  bottom();
                   bottom::top_show(); // call sub class top_show() method
                   $bottom_obj->top_look(); // call sub class top_look() method