PHP Fatal error: Call to a member function find() on a non-object however my function work

时间秒杀一切 提交于 2019-12-03 07:28:30

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!