php 致命错误:在非对象上调用成员函数 find()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18206803/
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
Fatal error: Call to a member function find() on a non-object
提问by user2677798
I am new to php.
I getting
我是 php 新手。
我得到
Fatal error: Call to a member function find() on a non-object error,
致命错误:在非对象错误上调用成员函数 find(),
I included simple_html_dom.php
我包括 simple_html_dom.php
<?php
include 'simple_html_dom.php';
$htm = file_get_html('http://www.thatscricket.com');
$es = $htm->find('div[class=score_card_display_below_links]');
$value = $es[0]->href;
$link = "http://www.thatscricket.com/$value";
$html = file_get_html('$link');
$scr = $html->find('span');
echo "$scr";
?>
采纳答案by Jim
$html = file_get_html('$link');
This will try to get the literal string '$link'
(variables aren't expanded inside single quoted strings). Which means $html
will be null or false.
这将尝试获取文字字符串'$link'
(变量不会在单引号字符串中展开)。这意味着$html
将是 null 或 false。
Since $html
isn't an object you can't call methods on it.
由于$html
不是对象,因此不能对其调用方法。
Use:
用:
$html = file_get_html($link);
You should also always check return types that may be false or null due to failure so that you can fail gracefully.
您还应该始终检查由于失败而可能为 false 或 null 的返回类型,以便您可以优雅地失败。
回答by Nishant
check the var_dump($htm)
first, it must be returning null
检查第var_dump($htm)
一个,它必须返回 null
回答by user3632390
I had also the same error I found it solution from php count function like this
我也遇到了同样的错误,我从这样的 php count 函数中找到了解决方案
if( $htm ){
$score_card_count = count($htm->find('div[class=score_card_display_below_links]'));
$score_card_count = trim($score_card_count);
if( $score_card_count > 0 )
{
$es = $htm->find('div[class=score_card_display_below_links]');
$value = $es[0]->href;
}
}
I hope it will work fine from this approach my error is fixed.
我希望它可以通过这种方法正常工作,我的错误已修复。
回答by som
$htm = file_get_html('http://www.thatscricket.com');
check var_dump($htm);
I think it returns bool(false)
检查var_dump($htm);
我认为它返回bool(false)
$html = file_get_html('$link');
should be
应该
$html = file_get_html($link);