PHP 继承构造函数不起作用



请检查我的问题 我制作了一个脚本$contractempsal但变量不起作用。 我想要员工工资,但我有错误。 请检查。

以下是我的代码:-

<?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 monthlysalaryy() {

$this->hourlyrate * $this->totalhours;
}
public function __construct($hourrate,$totalhoursd){
$this->hourlyrate="$hourrate";
$this->totalhours="$totalhoursd";
}   
}
$fulltimeemployee = new fulltimeemp("kannu"," singh");
$contractempsal = new contractemp("400","5");
echo $fulltimeemployee->getfullname();
echo $contractempsal->getfullname();
?>

所以,这是我的代码。 请检查并告诉我我错在哪里谢谢

请检查我的问题,我制作了一个脚本$contractempsal但变量不起作用。 我想要员工工资,但我有错误。 请检查。

您需要更新 constractemp 类__construct函数,然后调用parent::__construct来设置名字和姓氏。

<?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

最新更新