Inheritance in PHP


Concept

The basic idea behind this concept is when declaring classes, it's also possible to separate functionality into subclasses that automatically inherit the methods and variables of the class on which they are based. This can be useful if you're adding functionality to a class without modifying the original class.

The extends operator

When a class inherits from another class, the class from which it inherits is called the super class. When declaring a subclass, use the extends keyword to specify from which class it's inheriting.

Script for Inheritance PHP

<html>
<
body bgcolor="cyan">
<center>
<
h3> Inheritance with PHP <h3>
<
hr>
<?php
class Mike {
   
// How old the Mike is
    var $age;
   
function mike($new_age)
    {
       
// Set the age of  Mike to the n
        $this->age = $new_age;
    }
   
function Birthday(){
        $this->age++;
    }
}
class MikeSte extends Mike {
   
// Constructor
    function MikeSte()
    {
    }
   
// Sleep like a Mike
   
function sleep() {
   
echo("Mike is Presenting a inheritance with PHP.<br />");
    }
}
$fluffy=
new MikeSte();
$fluffy->Birthday();
$fluffy->sleep();
echo "Mike Age is $fluffy->age <br />";
?>
</center>
</body>
</
html>

Saved it by ext.php

Output of above script

To run the code, Open the XAMPP server and start the services like Apache and MySQL. Open the browser type: http://localhost/yourfoldername/ext.php 

Clipboard03.gif

The Parent Operator

Here in this section we will understand the inheritance with the parent operator, When extending classes to override functions in your class that are already defined in the super class, you can still execute the code from the parent class and then add on your own functionality.

Syntax

parent::method_from_parent

Script for Inheritance with Parent operator

<?php
class Cat {
     
// How old the cat is
      var $age;
     
function Cat($new_age){
           
// Set the age of this ca
            $this->age = $new_age;
      }
     
function Birthday(){
            $this->age++;
      }
     
function Eat(){
           
echo "Chomp chomp.";
      }
     
function Meow(){
           
echo "Meow.";
      }
}
class Domestic_Cat extends Cat {
     
// Constructor
      function Domestic_Cat() {
      }
     
// Eat like a Domestic_Cat
      function eat() {
           
parent::eat();
            // After we're finished eating, let's meow
            $this->meow();
     }    
}
    
?>

This calls the eat function from the super class, and then adds the code for meowing.

Tips

When you extend a class and declare your own constructor, PHP won't automatically call the constructor of the parent class. You should always call the constructor of the parent class to be sure all initialization code gets executed.

Lets see with an example

Calling the constructor of the parent class

<?php
    class Cat {
   
// How old the cat is
    var $age;
   
function Cat($new_age){
       
// Set the age of this cat to the new age
        $this->age = $new_age;
    }
   
function Birthday(){
        $this->age++;   
 }
   
function Eat(){
       
echo "Chomp chomp.";
    }
   
function Meow(){
       
echo "Meow.";
    }
}
class Domestic_Cat extends Cat {
   
// Constructor
    function Domestic_Cat($new_age) {
       
// This will call the constructor
        // in the parent class (the superclass)
        parent::Cat($new_age);
    }
}
?>

When a new instance of Domestic_Cat is created, the constructor from the Cat class is called.


Thanks !!

Up Next
    Ebook Download
    View all
    Learn
    View all