19 Program to demonstrate Inheritance in PHP

Program:


<?php

    class Person

    {

        var $name,$age;

       

        function set($name,$age)

        {

            $this->name = $name;

            $this->age = $age;

        }

       

        function display()

        {

            echo "Name: $this->name <br>";

            echo "Age: $this->age <br>";

        }

    }



    class Student extends Person

    {

        var $roll_no,$classe;

       

        function set1($name,$age,$roll_no,$classe)

        {

            parent::set($name,$age);

           

            $this->roll_no = $roll_no;

            $this->classe = $classe;

        }

       

        function display1()

        {

            parent::display();

           

            echo "Roll No: $this->roll_no <br>";

            echo "Class: $this->classe <br>";

        }

    }

   

    $s = new Student;

    $s->set1("Atharva",18,4,10);

    $s->display1();



   

?>


Output:


Program to demonstrate Inheritance in PHP

Previous
Next Post »