Overriding & Overloading Example using PHP

  1. class parentclass {  
  2.     function name() {  
  3.         return 'Parent';  
  4.     }  
  5. }  
  6.   
  7. class childclass extends parentclass {  
  8.       
  9.     function name() {  
  10.         return 'Child';  
  11.     }  
  12. }  
  13.   
  14. $obj = new childclass();  
  15. echo $obj->name();//It called child function instead of parent parent function - Overriding Example  
  16.   
  17.   
  18.   
  19.   
  20. class testclass {  
  21.     public $data;  
  22.     public function __get($name) {  
  23.           
  24.         echo "Getting '$name'\n ";  
  25.         return $this->data[$name];  
  26.           
  27.     }  
  28. }  
  29.   
  30. $obj = new testclass();  
  31. /** Magic method Example * */  
  32. echo $obj->b; //As __get function called - Overloading example 2  
  33. /** Magic method Example * */