Posted by CANbike on Mon, 13 Oct 2014

[PHP] Fatal error: Call to a member function find() on a non-object

The following is a solution to an issue encountered with the PHP Simple HTML DOM Parser

Fatal error: Call to a member function find() on a non-object

PHP Simple HTML DOM Parser was used to create a simple search function to parse tables on Some of these tables are growing in size with new data.

Unfortunately after updating the large “realestate” table, the search function no longer worked. The following message occured . . .

Fatal error: Call to a member function find() on a non-object in /../search.php on line 71

Checking the line revealed $html->find('') as the culprit; which seemed odd as the function worked in the past. Anyway, after checking the table there were no errors to be found. As a result, rows of data were removed from the table in order to test the find function again. Surprisingly everything worked.

The next test was to restore the original table and check the contents of $html = file_get_html('realestate.php');. Surprisingly the contents were empty. However, when the html table was reduced in size, the file was retrieved into the variable $html without issue.

Well, there was no memory issue. Obviously a file size limitation had been imposed.

Solution: Set a New PHP Simple HTML DOM Parser MAX_FILE_SIZE

Fortunately, the solution was simple. On line 66, of simple_html_dom.php was

define('MAX_FILE_SIZE', 600000);

a variable to define the maximum file size. An extra zero was added to the end, and the search function worked again!

Final Word: Error Checking

A solution was found, but a precautionary conditional statement should be added to check the variable html contents are set before calling find().

$html = file_get_html('realestate.php');

if($html && is_object($html) && isset($html->nodes)){
	foreach($html->find('') as $element){

1-Fatal error-Call to a member function find on a non-object-thumb.png2-PHP Simple HTML DOM Parser-MAX FILE SIZE-thumb.png3-search-thumb.png

Related Item(s):