How can the PHP function is_object() be used to prevent fatal errors related to non-object variables in DOM manipulation?

When manipulating the DOM in PHP, it's important to ensure that the variable being used is an object before attempting to access its properties or methods. Using the is_object() function can help prevent fatal errors that may occur when trying to manipulate non-object variables.

// Check if $element is an object before manipulating the DOM
if (is_object($element)) {
    // Perform DOM manipulation using $element
    $element->setAttribute('class', 'active');
} else {
    // Handle the case where $element is not an object
    echo 'Error: $element is not an object';
}