How can the error "Call to a member function on a non-object" be prevented when working with COM in PHP?

When working with COM objects in PHP, the error "Call to a member function on a non-object" can occur when trying to call a method on a variable that is not actually an object. To prevent this error, it is important to check if the COM object was created successfully before calling any methods on it. This can be done by verifying that the variable holding the COM object is not null before attempting to call any methods on it.

// Create a new instance of the COM object
$com = new COM('SomeCOMObject');

// Check if the COM object was created successfully before calling methods on it
if ($com) {
    // Call a method on the COM object
    $result = $com->SomeMethod();
} else {
    echo "Failed to create COM object";
}