PHP  inheritance

php inheritance is one of the fundamental mechanisms for code reuse in oops.

when one class is derived from another class this is called an inheritance. the new class (also called subclass, derived class, child class) can inherit members from the old class (also called superclass, parent class). the new class can also add additional properties and methods.

classes can inherit the methods and properties of another class using the extends keyword.

syntax:

         class     php_inheritance{   $model = null ; }
           
         class     car_inheritance    extends   Php_inheritance{           
            
                                                    $price = "$4000";  // $model property inherit from cars class                      
                                                 }

note : if a class extends another, then the parent class must be declared before the child class structure.


private member of the superclass are not inherited by the subclass and can only be indirectly accessed.

an inheritance defines the relationship between a superclass and its subclass. this means that an object of a subclass is-a superclass object.

 <?php  
           class    books{
              
                              public    $book_price  = null;
                      
                              private   $book_company  = null;
                      
                     
                 public     function   set_books_value( $cu, $com){
                      
                                            $this->book_price = $cu;
                                 
                                            $this->book_company = $com;
                                 
                                         }
                               
                 public     function   get_books_value(){
                      
                                     echo  "<br/>book price   = ". $this->book_price;
                                  
                                     echo  "<br/>book company  = ". $this->book_company;                                  
                                     
                                     }                              
                           }
                                                  
            class   php_book    extends   books{
             
                                                    private    $php_book_id = null;
                                                    
                                                    private    $php_book_company = null;
                                         
                                                    private    $php_book_price = null;
                                         
                 public   function   set_php_book_value( $i, $c, $p ){
                            
                                                                  $this->php_book_id = $i ;
                                           
                                                                  $this->php_book_company = $c ;
                                           
                                                                  $this->php_book_price = $p ;
                                           
                                                             }
                                      
                 public    function  get_php_book_value(){
                            
                             echo   "<br/> php book  id = ". $this->php_book_id ;
                                           
                             echo   "<br/> php book company = ". $this->php_book_company;
                                          
                             echo   "<br/> php book price = ". $this->php_book_price;                                          
                             
                                      }                                       
                             
                                   }                  
                                            
         $sub_obj  =  new  php_book();
                             
$sub_obj->set_php_book_value( "232f3", "headfirstphp", "$40" );
                             
$sub_obj->set_books_value( "$50", "headfirst" );//call the  super class set_books_value method
                             
$sub_obj->get_php_book_value();     // call the sub class get_php_book_value method
                             
$sub_obj->get_books_value();// call the super class get_books_value function
                             
$sub_obj->book_price;  // you can access super class book_price property
                             
         /* $sub_obj->.book_company

         you cannot access superclass  private properties and methods, because its have

         private visibility */     
                               
  ?>

note :- how call the super class methods in sub class method:

the parent keyword is used to call super class methods within derived class methods.

 <?php  
           class    cars_demo{
              
                              public    $country  = null;
                      
                              private    $company  = null;
                      
                     
                 public     function   set_value( $cu, $com){
                      
                                            $this->country = $cu;
                                 
                                            $this->company = $com;
                                 
                                         }
                               
                 public     function   get_value(){
                      
                                         echo  "<br/>car_demo country   = ". $this->country;
                                  
                                         echo  "<br/>car_demo company  = ". $this->company;                                  
                                     }                              
                               }
                                                  
            class   audi_demo    extends   cars_demo{
             
                                                    private    $audi_id = null;
                                                  
                                                    private    $audi_color = null;
                                         
                                                    private    $audi_price;
                                         
                 public   function   set_audi_value( $cu, $com, $ii, $cc, $pp ){
                 
                           parent::set_value( $cu, $com);
  
                           //above statement call the super class set_value() method
                            
                                                                         $this->audi_id = $ii ;
                                           
                                                                         $this->audi_color = $cc ;
                                           
                                                                         $this->audi_price = $pp ;
                                           
                                                                       }
                                      
                 public    function  get_audi_value(){
                 
                           parent::get_value();
                           
                           // above statement call the super class get_value method
                            
                                  echo   "<p> audi_demo  audi_id = ". $this->audi_id ."</p>";
                                           
                                  echo   "<p> audi_demo  audi_color = ". $this->audi_color ."</p>";
                                          
                                  echo   "<p> audi_demo  audi_price = ". $this->audi_price ."</p>";                                          
                                  
                                             }                                       
                                          }                  
                                            
         $sub_obj  =  new  audi_demo();
                             
         $sub_obj->set_audi_value("usa", "audi", "232f3", "green", "$40000" );
                             
         $sub_obj->get_audi_value();                             
                                      
  ?>

PHP instanceof

instanceof is used to determine whether a php variable is an instantiated object of a class, and object of a class that inherits from a parent class, and object of a class that implements an interface.

  <?php
  
             interface   cars{   }
             
             class     audi  implements  cars{    }
             
             class     tata   extends   audi {    }
             
             $tata_obj  =  new  tata();
             
        if( $tata_obj  instanceof  tata ) echo "instance of tata class" ;
             
        if( $tata_obj  instanceof  audi ) echo "<br/>instance of audi class <br/>";
             
        if( $tata_obj  instanceof  cars ) echo "instance of cars class";
        
            class   farari  {   }
            
            $farari_object  =  new   farari();
            
            if ( $farari_object  instanceof farari) ; // this statement return true
            
            if ( $farari_object  instanceof tata) ;  // this statement return false 
            
            if ( $tata_obj  instanceof farari) ;  // this statement return false
 
   ?>