Class Inheritance

Pre-requisites: Basic Understanding of Programming Language, MySQL.


In this project:

  • We will create a class.
  • The class will be having some properties and methods.
  • A new class will be created as a derived class from the class we have created above.
  • Derived class will inherit all the protected and public members of the base class. Private members are never inherited
  • Multiple objects will be created and accessed with the help of an array.

Type the following code in a PHP editor and save the file as Inheritance.php in Classes folder of your root directory.

<?php
class Person{
    protected $name;
    protected $age;
    
    public function __construct(){
        echo "Name --> ".$this->name;
        echo "Age --> ".$this->age;
    }

    public function putData($name, $age){
        $this->name = $name;
        $this->age = $age;
    }
}
class Student extends Person{
    private $rollNo;
    private $marks;
    public function __construct($name, $age, $rollNo, $marks){
        $this->name = $name;
        $this->age = $age;
        $this->rollNo = $rollNo;
        $this->marks = $marks; 
    }
    public function putStudentData(){        
        echo "<tr>";
        echo "<td>".$this->name."</td>";
        echo "<td>".$this->age."</td>";
        echo "<td>".$this->rollNo."</td>";
        echo "<td>".$this->marks."</td>";
        echo "</tr>";
    }
}
echo "<h3>STUDENT DETAILS ARE: </h3><br>";
echo "<table border='1' cellpadding='6' cellspacing='0'>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Roll No</th>
            <th>Marks</th>
        </tr>";
$students = array(
                    array("Smith", 26, 10, 99.4),
                    array("Chris", 25, 11, 90.8)
            );
for($i = 0; $i < count($students); $i++){
    $students[$i] = new Student($students[$i][0], $students[$i][1], $students[$i][2], $students[$i][3]);
}
for($i = 0; $i < count($students); $i++){
        $students[$i]->putStudentData();
}
echo "</table>";
?>

 Run Inheritance.php file in your browser to get the following output. URL may look like as follows:

Code Explanation of Inheritance.php

1.

class Person{

    protected $name;

    protected $age;

}

Class Person is a base class having properties name and age.

2.

class Student extends Person{

    private $rollNo;

    private $marks;

}

Class Student is derived from Class Person i.e. it will hold all the protected and public properties and methods of its base class along with its own new methods and properties.

 

For Example, here name and age will be inherited from Person Class and RollNo and marks are its own new members.

3.

public function __construct($name, $age, $rollNo, $marks){

        $this->name = $name;

        $this->age = $age;

        $this->rollNo = $rollNo;

        $this->marks = $marks; 

}

 

Here, we have used a constructor to initialize the variables automatically when the object is created.

4.

public function putStudentData(){        

        echo "<tr>";

        echo "<td>".$this->name."</td>";

        echo "<td>".$this->age."</td>";

        echo "<td>".$this->rollNo."</td>";

        echo "<td>".$this->marks."</td>";

        echo "</tr>";

}

 

putStudentData() method is used to display the required information on screen.

5.

$students = array(

                    array("Smith", 26, 10, 99.4),

                    array("Chris", 25, 11, 90.8)

            );

Information of two students is stored in an array. It can be increased or decreased as per the requirement.

6.

for($i = 0; $i < count($students); $i++){

    $students[$i] = new Student($students[$i][0], 

                                $students[$i][1], 

                                $students[$i][2], 

                                $students[$i][3]

                         );

}

For loop is used to initialize objects as an array.

Like, 

$students[$i][0]

$i refer to the object number.

0 refers to the Name variable.

7.

for($i = 0; $i < count($students); $i++){

        $students[$i]->putStudentData();

}

 

Again for loop is used to access the object array and display the data on screen.

 


Summary

In this project, we have learned to create a Base Class and then to derive a new Class inheriting the public and protected members of the base class.

We have also used the constructors to initialize the variables. An array $students is taken which is carrying all the details of the students and the objects are initialized and accessed as an array.    

Help Us to Improve our content

Let's Talk
Go back to Previous Course