我在代码的第71行遇到了这个错误,但这一行的函数执行正确,它做了我希望它做的事情。
然而,我注意到我的错误日志中充满了以下几行:
〔09-Dec-2013 14:54:02 UTC〕PHP致命错误:在第71行上调用/home/sportve/public_html/open_event_common.PHP中的非对象上的成员函数find()
我检查了什么:
CCD_ 1已经包括在内,并且71号线打算做的这个功能正在起作用。
这是我的代码第71行:
$content->find('a.openevent', 0)->innertext = '';
因此,是什么原因导致这个错误出现在我的错误日志文件中,这让人感到困惑?
编辑:这里是完整的代码:
<?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';
?>
根据您提供的信息,最好、最实用的解决方案是简单地检查$html
和$content
是否为空。
当你在非对象上得到"对成员函数的调用[无论函数是什么]"时,10次中有9次,这基本上意味着该对象不存在。意味着变量为空。这是您重新编写的代码:
$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';
}
}
}
此外,我添加了一个检查,查看$url
是否也是空的。