sea26.2
sea26.2

Reputation: 380

"Undefined property: stdClass"

This error suddenly occurred when Pressflow was added to our Drupal installation. It is coming from a custom module that, prior to Pressflow seemed to work fine. After the addition of Pressflow, running Drush causes this error to display on screen.

The function that is identified as the source of the error looks like this:

function user_search_location_get(&$user) {
  if (count($user->user_location_pref)) {  // This line is causing the error.
    return $user->user_location_pref;
  }

  // …
}

The error message is the following:

WD php: Notice: Undefined property: stdClass::$user_location_pref in user_search_location_get()

Upvotes: 1

Views: 7186

Answers (1)

Coder1
Coder1

Reputation: 13321

Short answer, in your custom module, you should check if that property exists before you count it. That, or make sure the $user object has that property before you use it.

if (isset($user->user_location_pref) && count($user->user_locaion_pref) > 0) {
  return $user->user_locaion_pref;
}

While it is a little more work, when you start developing with notices turned on, you will find errors in your code that otherwise would have not appeared till later, and would have been more difficult to track down.

In your previous environment or install, the PHP error reporting was probably set to not show notices. While I recommend keeping notices on and making your code work with them, you can turn them off through the Drupal 7 UI. Configuration -> Development -> Logging and Errors. Then set the value to 'Errors and Warnings' ... Otherwise, you can set your error reporting level in your php.ini to report all except notices.

Note that Drupal 6 did not force notice reporting on, while Drupal 7 does. That prompts this type of question a lot.

If this is your only notice issue though, it makes more sense to just correct your custom module.

Upvotes: 2

Related Questions