[Solved] PHP inheritance constructor function didn’t work


You need to update __construct function of constractemp class for then call parent::__construct to set first and last name.

<?php
class baseemp {

    protected $firstname;
    protected $lastname;

    public function getfullname() {
        return $this->firstname .''. $this->lastname; 
    }   

    public function __construct($first,$last) {
        $this->firstname="$first";
        $this->lastname="$last";
    }   
}

class fulltimeemp extends baseemp {

    public $monthlysal;

    public function monthlysalary() {
        $this->monthlysal/12;
    }   
}

class contractemp extends baseemp {

    public $hourlyrate;
    public $totalhours;

    public function monthlysalary() {
        return $this->hourlyrate * $this->totalhours;
    }
    public function __construct($first,$last, $hourrate, $totalhoursd){
        parent::__construct($first,$last);
        $this->hourlyrate="$hourrate";
        $this->totalhours="$totalhoursd";
    }   
}

$fulltimeemployee = new fulltimeemp("kannu"," singh");
$contractempsal = new contractemp("kannu"," singh", "400","5");

echo $fulltimeemployee->getfullname(); // display kannu singh

echo $contractempsal->getfullname(); // display kannu singh

echo $contractempsal->monthlysalary(); // display 2000

12

solved PHP inheritance constructor function didn’t work