PHP 致命错误:在非对象上调用成员函数 find() 但是我的函数可以工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20473748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: Call to a member function find() on a non-object however my function work
提问by user3080061
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.
我在代码的第 71 行收到此错误,但是此行的功能已正确执行,并且执行了我期望的操作。
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
[09-Dec-2013 14:54:02 UTC] PHP 致命错误:在第 71 行的 /home/sportve/public_html/open_event_common.php 中的非对象上调用成员函数 find()
What I have checked for:
我检查过的内容:
simple_html_dom_parseris already included and this function that Line 71 intend to do is working.
simple_html_dom_parser已包含在内,第 71 行打算执行的此功能正在运行。
Here is Line 71 of my code:
这是我的代码的第 71 行:
$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';
?>
回答by JakeGould
Based on the information you're providing the best and most practical solution is to simply do a check to see if $htmland $contentis empty or not.
根据您提供的信息,最好和最实用的解决方案是简单地检查$html和$content是否为空。
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:
当您收到“调用非对象上的成员函数[无论函数是什么]”时,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';
}
}
}
Also, I added a check to see if the $urlis empty as well.
另外,我还添加了一个检查以查看是否$url为空。

![php SQLSTATE[HY093]:参数号无效:没有绑定参数,但提供了参数](/res/img/loading.gif)