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.

 

Class Inheritance

  

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"261099.4),

                    array("Chris"251190.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

© 2021 Lecturely.com. All rights reserved.

Lecturely.com – Imagined by Paras Jairath, Developed by P3 Multisolutions & Powered by Network Bulls.