Error Call to Member Function get on null III

More efficient code for the sanity check preventing the "Error call to member function get on null."


In the last article, we saw a somewhat inefficient way to prevent the "Call to Member Function get on null" in MODX. In this one, we'll see a faster way.


MODX logo

For review, here's the code from the previous article:

In the code below, we check the $parentObject variable to make sure it's not null:

$docs = $modx->getCollection('modResource');
foreach ($docs as $doc) {
    $parentId = $doc->get('parent');
    $parentObject = $modx->getObject('modResource', 
        $parentId);
    // or $parentObject = $doc->getOne('Parent');
    if ($parentObject !== null) {
        $output .= '<li>' .
            $parentObject->get('pagetitle') .
            '</li>';
    } else {
        /* Resource has no parent --
           Do something else or do nothing */
    }
}

return $output;

Speeding Things Up

As we said in the previous article, the code above is somewhat inefficient. It attempts to get the parent of every resource, whether it has one or not. That's a waste of time.

We can prevent that easily if we just check the value of the parent field and skip the attempt if it's empty (i.e., there's no parent). If you want to do nothing when there's no parent, the solution is fast and easy. Just use a continue statement to skip the rest of the loop in that case:

$docs = $modx->getCollection('modResource');
foreach ($docs as $doc) {
    $parentId = $doc->get('parent');

    if (empty($parentId)) {
        continue;
    }
    $parentObject = $modx->getObject('modResource', 
        $parentId);
    // or $parentObject = $doc->getOne('Parent');
    if ($parentObject !== null) {
        $output .= '<li>' . 
            $parentObject->get('pagetitle') . 
            '</li>';
    } else {
        /* Resource has no parent --
           Do something else or do nothing */
    }
}

return $output;

But what if you want to do "something else" when there's no parent? That gets a little trickier:

$docs = $modx->getCollection('modResource');
foreach ($docs as $doc) {
    $parentId = $doc->get('parent');
    $parentObject = null;

    if (!empty($parentId)) {
        $parentObject = $modx->getObject('modResource', 
            $parentId);
        // or $parentObject = $doc->getOne('Parent');
    }
    if ($parentObject !== null) {
        $output .= '<li>' . 
            $parentObject->get('pagetitle') . 
            '</li>';
    } else {
        /* No parent -- do something else */
    }
}

return $output;

Why test $parentObject even if the $parent field is not empty? Because the $parent field may not point to an existing resource. If a resource is deleted in PhpMyAdmin or deleted improperly in code, it's children will still have parent fields that point to it even though it no longer exists. I've also seen invalid parents caused by dragging resources around in the tree in earlier versions of MODX. Notice that we've set $parentObject to null at the top of the foreach loop. If the $parent field is empty, it will remain null, if the getObject() or getOne() call fails it will also be null.

It's a good practice to try to design your code so that it will never cause a PHP error, no matter what happens. That means adding a "sanity check" whenever a bit of code might cause an error, however unlikely it is. In a production environment, you should have PHP set to *not* display errors. That means that when things crash, it will be extremely difficult to figure out what happened. Checking a variable to make sure it's not empty or null takes just a few milliseconds. It's definitely worth it if it will prevent your code from crashing.


Another Improvement

There's one remaining problem with our code. If the $parent field is invalid, the program goes merrily along. No error is thrown, but there's no way for us to know that we have one or more resources with an invalid parent field. We'll fix that in the next article.


Coming Up

In the next article, we'll add an error message that will alert us to any invalid parent fields in the retrieved resources.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)