CodingBison

Object-oriented programming allows a new class to be built on top of an existing class. This is known as inheritance.

The new class inherits all variables and methods present in the parent class. In addition, it is free to add some of its own! We refer to the new class as derived class and we refer to the the base class as the parent class. This allows the derived class to simply focus on adding extra functionality and not have to worry about what is already offered by the parent class.

The derived class also inherits the constructor method of the parent class. However, if the derived class redefines its own constructor, then when we create an object from the derived class, PHP calls the constructor of the derived class and does not call the constructor of the parent class.

We provide a simple program to demonstrate inheritance.

This program (provided below) defines a new class (SocialUser) that inherits a base class (BasicUser). Next, the SocialUser class extends the parent class by providing additional functionality to add/print friends. Since SocialUser inherits BasicUser class, it can automatically call BasicUser's print_userinfo() method without having to redefine it.

 <?php
 /* BasicUser is the base (parent) class */
 class BasicUser {
     function __construct($varID, $varName, $varAge) {
         echo "<br>Initializing the BasicUser object...";
         $this->userID = $varID;
         $this->userName = $varName;
         $this->userAge = $varAge;
     }   

     function print_userinfo() {
         echo "<br>Printing User Details: <br>";
         echo "Userid: $this->userID<br>";
         echo "Name: $this->userName<br>";
         echo "Age: $this->userAge<br>";
     }   
 }

 /* SocialUser is the derived class */
 class SocialUser extends BasicUser {
     var $friends;

     function __construct($varID, $varName, $varAge) {
         echo "<br>Initializing the SocialUser object...";
         $this->userID = $varID;
         $this->userName = $varName;
         $this->userAge = $varAge;
     }

     function add_friend($varNewFriend) {
         $this->friends[] = $varNewFriend;
     }   

     function print_friends() {
         echo "<br> ---- [Start] Printing Details for all friends ----- ";
         foreach ($this->friends as $element) {
             $element->print_userinfo();
         }
         echo "<br> ---- [End] Printing Details for all friends ----- ";
     }   
 }

 /* Create a new SocialUser object */
 $new_user = new SocialUser("ksimon", "Karuna Simon", 32);
 $new_user->print_userinfo();

 /* Create two friends of BasicUser class */
 $my_friend = new BasicUser("jfulton", "James Fulton", 21);
 $my_friend = new BasicUser("amoore", "Amanda Moore", 33);

 /* Add them to the SocialUser list */
 $new_user->add_friend($my_friend);
 $new_user->add_friend($my_friend);

 /* Print friends info for the SocialUser object */
 $new_user->print_friends();
 ?>

The output (provided below) shows that when we create $new_user from SocialUser class, then it calls __construct() of SocialUser and not that of BasicUser. However, if we were to not define the __construct() for SocialUser, then $new_user would instead call the __construct() of the BasicUser.

 Initializing the SocialUser object...
 Printing User Details:
 Userid: ksimon
 Name: Karuna Simon
 Age: 32

 Initializing the BasicUser object...
 Initializing the BasicUser object...
 ---- [Start] Printing Details for all friends -----
 Printing User Details:
 Userid: amoore
 Name: Amanda Moore
 Age: 33

 Printing User Details:
 Userid: amoore
 Name: Amanda Moore
 Age: 33

 ---- [End] Printing Details for all friends ----- 

If needed, a derived class can redefine a method that is already defined by its parent class. For example, in the above program, the SocialUser class could redefine print_userinfo() method. When a derived class redefines a method, then for the derived class, PHP calls the redefined method and not the parent's method; this behavior is consistent with that of the __construct() method!

However, if there is still a need to call the parent's method, then the derived class can do so by either using the parent keyword or by directly using the class name. Thus, to call BasicUser's print_userinfo() from the derived SocialUser class, we can use either "parent::print_userinfo()" or "BasicUser::print_userinfo()". We recommend avoiding hard-coding (as in "BasicUser::print_userinfo()") since it is possible that the name of the parent class may change in future.





comments powered by Disqus