after PHP upgrade - Cannot use object of type stdClass as array

358 views Asked by At

After PHP Upgrade I get this Error, on many points:

[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);
        }
1

There are 1 answers

0
lxg On BEST ANSWER

$employee_hours is obviously an object. However, with the $employee_hours[$this->selected_day - 1] notation, you're trying to use it as an associative array – which is invalid.

If the $employee_hours can be of different types (which is not desirable btw), you should at least check that it is an array before using it as one:

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' ))
) {
    // …
}

By the way, you say that this happens since you've upgraded your PHP install. I just made a quick test with various PHP versions, the only version that does not trigger a fatal error is PHP 4.x … all of the PHP 5.x versions produce a fatal error.