我想使用一个类,确定薪水和人数与薪水,OOP是新的我,我已经通过大量的教程和示例,但我不知道如果我使用以下最佳实践:PHP OOP最佳做法指定要调整的属性
class quickCalc
{
private $salary = 0;
private $person = 0;
private $person_salary = 0;
private animal = 0;
private animal_salary = 0;
public function addCost($type, $salary)
{
$this->$type++;
$type_two = $type.'_salary';
$this->$type_two += $salary;
}
public function getCount($type)
{
return $this->$type;
}
public function getCost($type)
{
$type = $type.'_salary';
return $this->$type;
}
}
$test = new quickCalc();
$test->addCost('person', 15000);
echo $test->getCount('person'); //1
echo $test->getCost('person'); //10000
echo $test->getCount('animal'); //0
因此可以看出,为我添加了一个人,或动物等,由1中的相关属性的增加,而且薪水值添加到现有值
我不确定的是,如果最好的做法是通过对象传递属性名称,如图所示:
public function addCost($type, $salary)
{
$this->$type++;
}
或应每个属性都有其自身的功能,比如这个:
public function addPerson($salary)
{
$this->person++;
$this->person_salary++;
}
这个问题似乎更适合http://codereview.stackexchange.com – JimL
你可以或者仍然始终_call_同样的方法,但使用'开关'语句,然后为所述类型运行关联的方法。 – Brett