8

I am getting this error on Line 71 of my code, however the function of this line is executed correctly and it does what I expect it to do.

However, I noticed that my error log is full of these lines:

[09-Dec-2013 14:54:02 UTC] PHP Fatal error: Call to a member function find() on a non-object in /home/sportve/public_html/open_event_common.php on line 71

What I have checked for:

simple_html_dom_parser is already included and this function that Line 71 intend to do is working.

Here is Line 71 of my code:

$content->find('a.openevent', 0)->innertext = '';

so its confusing as to what is causing this error to appear in my error log file?

Edit: here is the full code:

<?php       
    $url = "static/" . $cat_map[$cat]['url'];
    $html = file_get_html($url);
    $content = $html->find('div#event-pane > div#e' . $event_id, 0);
    $content->find('a.openevent', 0)->innertext = '';
    $content->find('h3.lshtitle', 0)->onclick = '';
    $content->find('h3.lshtitle', 0)->tag = 'div';
    $content->find('div.lshtitle', 0)->class = 'ttl';                
?>
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
user3080061
  • 83
  • 1
  • 1
  • 7
  • `$content` is not an object. You need to show the rest of the relevant code – Marc B Dec 09 '13 at 15:03
  • Show where and how `$content` is set. If that contains variables, show what is in those variables at that time (`var_dump(..)`) – Sumurai8 Dec 09 '13 at 15:04
  • @Marc B check it again i have updated the question – user3080061 Dec 09 '13 at 15:08
  • @Sumurai8 check it again i have updated the question – user3080061 Dec 09 '13 at 15:08
  • 2
    After you create the `$content` variable, you need to make sure your call to `$html->find()` actually returned what you expected it to. If it didn't find any elements, then it stands to reason any of the subsequent `$content->find()` calls won't work. – WWW Dec 09 '13 at 15:09
  • if find() fails, it will NOT return an object. it'll return a boolean false. You're simply assuming that the first find works. Since $content is not an object, the find obviously failed. – Marc B Dec 09 '13 at 15:11
  • Look at the [documentation](http://simplehtmldom.sourceforge.net/manual_api.htm) and [the manual](http://simplehtmldom.sourceforge.net/manual.htm). `->find( str, int )` returns `NULL` if the element doesn't exist. Calling something on `NULL` will give an error (obviously). – Sumurai8 Dec 09 '13 at 15:16

1 Answers1

18

Based on the information you’re providing the best and most practical solution is to simply do a check to see if $html and $content is empty or not.

9 times out of 10 when you get a “Call to a member function [whatever the function is] on a non-object” that basically means the object just doesn’t exist. Meaning the variable is empty. Here is your code reworked:

$url = "static/" . $cat_map[$cat]['url'];
if (!empty($url)) {
  $html = file_get_html($url);
  if (!empty($html)) {
    $content = $html->find('div#event-pane > div#e' . $event_id, 0);
    if (!empty($content)) {
      $content->find('a.openevent', 0)->innertext = '';
      $content->find('h3.lshtitle', 0)->onclick = '';
      $content->find('h3.lshtitle', 0)->tag = 'div';
      $content->find('div.lshtitle', 0)->class = 'ttl';
    }
  }
}

Also, I added a check to see if the $url is empty as well.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103