Topic: [answered] catching ->get errors  (Read 328 times)

Pages: [1]   Go Down

#1: 4-Jan-2010, 07:32 PM

oori
Posts: 51

I have a schema extention for users (extends modUser).
Some users (admin..) do not have that extended dataset.

For example, with this code (fragment of a output filter snippet):
Code:
$memberSchemaDef = '{"'.$class.'":{}}';
$memberUser = $modx->getObjectGraph('memberUser', $memberSchemaDef , $input);
if (isset($memberUser)) {
 return $memberUser->{$class}->get($field);   // <-- fails here
}

This code will fail once it's executed on the admin user (or another user without the requested field).
Code:
Fatal error: Call to a member function get() on a non-object

Ofcourse - this makes sense, but how can I catch these errors?  I don't want thme to break the scripts.

Naturally, I can add specific exclution for admin user, but I'm looking for a generic solution.    php try/catch will not help (as that would have needed to be implemented deeper in the core).    isset/empty also will not, as $memberUser IS set for admin (as memberUser class extends modUser).

Idealy I'd like to make ->get return '' empty, instead of error..

Thanks.
« Last Edit: 4-Jan-2010, 09:37 PM by oori »

#2: 4-Jan-2010, 08:30 PM

Foundation

splittingred
Posts: 1,500

i am alt-country rock

WWW
Code:
if (!($memberUser->{$class} instanceof $class)) {
   // do error handling
}
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#3: 4-Jan-2010, 09:37 PM

oori
Posts: 51

Thanks for your quick reply.
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.