Saturday, 22 May 2021

Php: Constructors and inheritance

Just like other instance method inheritance, constructor methods also get inherited to sub classes.

 

constructor_inheritance_demo.php

#!/usr/bin/php

<?php

    class Employee{
        public $id;
        public $name;
        public $age;

        public function __construct($id, $name, $age){
            $this->id = $id;
            $this->name = $name;
            $this->age = $age;
        }

        public function __toString(){
            return "id : $this->id\nname : $this->name\nage: $this->age\n";
        }
    }

    class Manager extends Employee{
        public $no_of_reportees;
     
        public function __toString(){
            return parent::__toString(). "no_of_reportees : $this->no_of_reportees\n";
        }
    }

    $manager1 = new Manager(1, 'Krishna', 31);
    $manager1->no_of_reportees = 3;
    $info = (string)$manager1;

    echo $info;

?>

Output

$./constructor_inheritance_demo.php 

id : 1
name : Krishna
age: 31
no_of_reportees : 3




 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment