我是一个PHP新手试图简化我的代码以提高可读性。我正在学习OOP,并希望使用extends
使用employee
类的一些相同字段创建person
类。但是,当我这样做时,我最终会反复提出许多论点。
我是否将extends
用于其目的,或者我只是过于复杂化了?我想在相关类之间传递参数时避免冗余。请看下面的代码:
<?php
//CREATE CLASS FOR EMPLOYEE
class person {
function __construct($fName,$lName,$age,$address){
$this->firstName=$fName;
$this->lName=$lName;
$this->age=$age;
$this->address=$address;
}
}
class employee extends person{
function __construct($balance,$goal,$fName,$lName,$age,$address){
parent::__construct($fName,$lName,$age,$address);
$this->balance=$balance;
$this->goal=$goal;
$this->fillRoster($balance,$goal,$fName,$age,$address);
}
protected function fillRoster($fName,$age,$address){
//$sql="INSERT INTO employee (balance,goal,firstName,lastName,age,address) VALUES ($balance,$goal,$fName,$lName,age,$address)";
echo "hello";
}
}
?>