我似乎被困住了。我有一个计算工资的功能。我正在努力做到这一点,以便在调用函数时可以指定一个或多个用户。但是我遇到了两个问题。一种是如果我指定多个用户ID,如何构造输出结果。第二个问题是如何(对于一个和许多用户)不重复两次我的代码。如果我只需要一个用户就可以使用它。
我希望能够这样称呼它:
// Get salary for user id: 10
$salary = Salary::getSalary([10], '2018-12-01', '2018-12-31');
echo $salary->user->fullname;
echo $salary->salary->total;
echo $salary->checked;
// Get salary for users - 10, 20, 30
$salaries = Salary::getSalary([10, 20, 30, '2018-12-01', '2018-12-31');
foreach ($salaries as $salary) {
echo $salary->user->fullname;
echo $salary->salary->total;
echo $salary->checked;
}
这是我的职能
public function getSalary($user_id, $date_from, $date_to)
{
$salary = new stdClass;
if ( count($user_id) == 1 ) {
$salary->user = new stdClass;
$salary->user->fullname = self::getUserById($user_id)->fullname;
$salary->user->phone = self::getPhone($user_id);
$salary->user->email = self::getUserById($user_id)->e_mail;
$salary->salary = self::getSalary($user_id, $date_from, $date_to);
$salary->checked = self::isChecked($user_id, $date_from, $date_to);
} else if ( count($user_id) > 1 ) {
foreach ($user_id as $employee)
{
$salary->employee = new stdClass;
$salary->employee->fullname = self::getUserById($user_id)->fullname;
$salary->employee->phone = self::getPhone($user_id);
$salary->employee->email = self::getUserById($user_id)->e_mail;
$salary->employee->siawork = self::getUserById($user_id)->siawork;
$salary->salary = self::getSalary($user_id, $date_from, $date_to);
$salary->checked = self::isChecked($user_id, $date_from, $date_to);
}
}
return $salary;
}
答案 0 :(得分:1)
我认为如果调用该函数并且只有1个条目,那么可以将其转换为数组,以便始终使用相同的代码来处理数据,并构建薪水数据的数组以发回...
public function getSalary($user_id, $date_from, $date_to)
{
$salaries = [];
if ( !is_array($user_id) ) {
$user_id = [$user_id];
}
foreach ($user_id as $employee)
{
$salary->employee = new stdClass;
$salary->employee->fullname = self::getUserById($user_id)->fullname;
$salary->employee->phone = self::getPhone($user_id);
$salary->employee->email = self::getUserById($user_id)->e_mail;
$salary->employee->siawork = self::getUserById($user_id)->siawork;
$salary->salary = self::getSalary($user_id, $date_from, $date_to);
$salary->checked = self::isChecked($user_id, $date_from, $date_to);
$salaries[] = $salary;
}
return $salaries;
}
如果愿意,可以通过修改代码的最后一部分来返回单个条目...
if ( count($user_id) == 1 ) {
$salaries = $salaries[0];
}
return $salaries;
}
答案 1 :(得分:0)
这是我为您的类函数提供的解决方案。似乎您正在尝试返回单个对象并每次都覆盖员工。如果您可以接受ID数组,则可以返回一个薪金数组。这有道理吗?
public function getSalary($user_id, $date_from, $date_to)
{
//Return an array of salary objects to match your array of id inputs
$salary = [];
if ( ! is_array($user_id) ) {
//In case a non-array is entered
$user_id = [$user_id];
}
// A foreach will work even if there's only one user ID given,
// as long as it's an array (which it should be by this point)
foreach ($user_id as $id)
{
//Get your salary object by id
$this_user = new stdClass;
$this_user->user = new stdClass;
$this_user->user->fullname = self::getUserById($id)->fullname;
$this_user->user->phone = self::getPhone($id);
$this_user->user->email = self::getUserById($id)->e_mail;
$this_user->salary = self::getSalary($id, $date_from, $date_to);
$this_user->checked = self::isChecked($id, $date_from, $date_to);
//Put it in the output array
$salary[] = $this_user;
}
return $salary;
}
注意,请验证所有这些输入!确保在$user_id
或$date_from
或$date_to
中放入意外内容不会破坏所有内容,至少不会显示错误。