[英]Error in PHP : "Trying to get property of non-object"
我在第 44 行尝试在C:\\xampp\\htdocs\\Parser\\class.php
获取非对象的属性时出现错误 这是我的课程:
class Parser {
public $links = array(
'infosecurity' => '',
'programming' => '',
'webdev' => '',
'linux' => '',
'algorithms' =>''
);
public $headers = array(
'infosecurity' => '',
'programming' => '',
'webdev' => '',
'linux' => '',
'algorithms' =>''
);
public $texts = array(
'infosecurity' => '',
'programming' => '',
'webdev' => '',
'linux' => '',
'algorithms' =>''
);
public function get_article_link($page_link, $tag, $type) {
$html = get_curl($page_link);
$dom = str_get_html($html);
$article = $dom->find($tag);
return $article[0]->$type.'<br>'; //line 44
}
public function get_content($page_link, $tag, $type) {
$html = get_curl($page_link);
$dom = str_get_html($html);
$article = $dom->find($tag);
return $article[0]->$type.'<br>';
}
}
这是我调用该方法的代码部分:
$title = $Habr ->get_content($Habr->links["$key"], 'title', 'plaintext');
$str = strpos($title, "/");
$title = substr($title, 0, $str);
$Habr->headers[$key] = $title;
echo "<br><b>TITLE : </b><br>".$title."<br>";
$text = $Habr ->get_content($Habr->links[$key],'.post__text','plaintext');
$Habr->texts[$key] = $text;
echo "<b>TEXT OF PAGE:<br></b>".$Habr->texts[$key]."<br>";
}
但是当我在调用函数时使用字符串链接时,例如
$title = $Habr ->get_content("HERE LINK", 'title', 'plaintext');
它工作正常。 我该如何解决这个错误?
这是var_dump($Habr->links); 的结果。
array(5) { ["infosecurity"]=> string(55) "https://habrahabr.ru/company/kaspersky/blog/348572/
" ["programming"]=> string(50) "https://habrahabr.ru/company/2gis/blog/348510/
" ["webdev"]=> string(52) "https://habrahabr.ru/company/skyeng/blog/348606/
" ["linux"]=> string(51) "https://habrahabr.ru/company/flant/blog/348324/
" ["algorithms"]=> string(37) "https://habrahabr.ru/post/348530/
" }
我会使用调试器并在使用断点调用 get_content 调用之前检查所有输入的值。 如果您没有调试器,请添加一个日志功能以将您的变量写入日志。 您可能会发现其中一个或多个未初始化。 那会导致你的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.