PHP 升级后 - 无法将 stdClass 类型的对象用作数组
after PHP upgrade - Cannot use object of type stdClass as array
在 PHP 升级后我得到这个错误,在很多方面:
[Wed Jun 10 22:50:16 2015] [error] [client 10.0.7.85] PHP Fatal error: Cannot use object of type stdClass as array in /var/www/bp/apps/frontend/modules/timekeeping/actions/actions.class.php on line 1275, referer: http://10.0.0.244/timekeeping/overviewAbsent
if (isset($employee_hours[$this->selected_day - 1]->special) && in_array($employee_hours[$this->selected_day - 1]->special, array( 'u', 'm', 's', 'b', 'kk', 'k', 'f', 'fb', 'uf' ))) {
array_push($this->employees, $employee);
}
$employee_hours
显然是一个对象。但是,使用 $employee_hours[$this->selected_day - 1]
表示法,您试图将其用作关联数组——这是无效的。
如果 $employee_hours
可以是不同的类型(顺便说一句,这是不可取的),你至少应该在将它用作一个数组之前检查它是一个数组:
if (
is_array($employee_hours) &&
isset($employee_hours[$this->selected_day - 1]) &&
isset($employee_hours[$this->selected_day - 1]->special) &&
in_array($employee_hours[$this->selected_day - 1]->special, array( 'u', 'm', 's', 'b', 'kk', 'k', 'f', 'fb', 'uf' ))
) {
// …
}
顺便说一句,你说这是因为你升级了 PHP 安装。我刚刚用 various PHP versions 做了一个快速测试,唯一没有触发致命错误的版本是 PHP 4.x …所有 PHP 5.x 版本都产生致命错误。
在 PHP 升级后我得到这个错误,在很多方面:
[Wed Jun 10 22:50:16 2015] [error] [client 10.0.7.85] PHP Fatal error: Cannot use object of type stdClass as array in /var/www/bp/apps/frontend/modules/timekeeping/actions/actions.class.php on line 1275, referer: http://10.0.0.244/timekeeping/overviewAbsent
if (isset($employee_hours[$this->selected_day - 1]->special) && in_array($employee_hours[$this->selected_day - 1]->special, array( 'u', 'm', 's', 'b', 'kk', 'k', 'f', 'fb', 'uf' ))) {
array_push($this->employees, $employee);
}
$employee_hours
显然是一个对象。但是,使用 $employee_hours[$this->selected_day - 1]
表示法,您试图将其用作关联数组——这是无效的。
如果 $employee_hours
可以是不同的类型(顺便说一句,这是不可取的),你至少应该在将它用作一个数组之前检查它是一个数组:
if (
is_array($employee_hours) &&
isset($employee_hours[$this->selected_day - 1]) &&
isset($employee_hours[$this->selected_day - 1]->special) &&
in_array($employee_hours[$this->selected_day - 1]->special, array( 'u', 'm', 's', 'b', 'kk', 'k', 'f', 'fb', 'uf' ))
) {
// …
}
顺便说一句,你说这是因为你升级了 PHP 安装。我刚刚用 various PHP versions 做了一个快速测试,唯一没有触发致命错误的版本是 PHP 4.x …所有 PHP 5.x 版本都产生致命错误。