PHP Fatal error: Call to a member function find() on a non-object however my function work
You are staring at a PHP Fatal error: Call to a member function find() on a non-object message, yet your script seems to execute without crashing immediately. This disconnect between the error log and runtime behavior is a classic symptom of silent failures in DOM parsing libraries, specifically when using simple_html_dom. The library doesn't throw an exception; it returns false or null when a selector yields no results. When you chain method calls on that return value without verifying it first, PHP throws this fatal error the moment it tries to invoke a method on a non-existent object.
In your specific case, the culprit is not necessarily the line causing the crash, but the state of the variable passed into it. Look at this snippet:
$content = $html->find('div#event-pane > div#' . $event_id, 0);
$content->find('a.openevent', 0)->innertext = '';
The simple_html_dom_parser library is included and your find() function works when called on $html. However, the selector 'div#event-pane > div#' . $event_id' might not match any element in the fetched HTML. If the DOM structure of the target page differs slightly from what you expect, or if $cat_map[$cat]['url'] points to a page where that specific div doesn't exist, find() returns false.
When $content is false, attempting to call $content->find() triggers the fatal error. The confusing part is that your script might not halt entirely if other parts of your application are isolated or if you are using a shared hosting environment where the web server buffers output. You see the error in the log because PHP stops execution at that specific line, but if this code runs inside a larger loop or a background job, the process might continue to other tasks before terminating, making it appear as though the function "worked" generally.
To fix this, you must explicitly check whether the parent object exists before attempting to parse its children. This defensive coding pattern prevents the fatal error from halting your entire application flow unexpectedly. Here is how you should refactor the logic:
$target = $html->find('div#event-pane > div#' . $event_id, 0);
// Check if the initial selector found anything
if ($target) {
// Now safe to manipulate children
$target->find('a.openevent', 0)->innertext = '';
$target->find('h3.lshtitle', 0)->onclick = '';
$target->find('h3.lshtitle', 0)->tag = 'div';
$target->find('div.lshtitle', 0)->class = 'ttl';
} else {
// Handle the case where the container doesn't exist
// Log a warning or set a default state instead of crashing
error_log("Event pane not found for ID: " . $event_id);
}
By wrapping the operations in an if block, you ensure that $content is actually an object before calling methods on it. This approach isolates failures to specific events or pages rather than bringing down the whole script. It also gives you a chance to log meaningful errors instead of just seeing a generic stack trace in your error log.
Additionally, consider using trim() when setting inner text or tags to avoid whitespace issues that sometimes cause parsing inconsistencies in older versions of simple_html_dom. If you are maintaining legacy codebases with strict PHP version requirements, be aware that newer versions of the library have stricter type checking which might alter how false is handled in certain contexts.
If you need a more robust solution for scraping dynamic content where DOM structures vary frequently, integrating a modern framework like Laravel can help. The ecosystem offers better abstractions for handling optional elements without resorting to manual null checks everywhere. For instance, using Laravel's collection helpers or Eloquent relationships provides clearer error handling than raw simple_html_dom calls in many scenarios. While your current setup works for static pages, adding these checks ensures stability when the target HTML changes.