We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22295
    • 153 Posts
    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):
    $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).
    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.
      • 28215
      • 4,149 Posts
      if (!($memberUser->{$class} instanceof $class)) {
         // do error handling
      }
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 22295
        • 153 Posts
        Thanks for your quick reply.