简体   繁体   English

PHP 中的错误:“试图获取非对象的属性”

[英]Error in PHP : "Trying to get property of non-object"

I have an error Trying to get property of non-object in C:\\xampp\\htdocs\\Parser\\class.php on line 44 Here is my class:我在第 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>';              
        }
    }

Here is a code part where I call the method:这是我调用该方法的代码部分:

 $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>";         
        }

But when I use string link when I call the function, for example但是当我在调用函数时使用字符串链接时,例如

 $title =  $Habr ->get_content("HERE LINK", 'title', 'plaintext');

it works fine.它工作正常。 How can I solve this error?我该如何解决这个错误?

Here is result of var_dump($Habr->links);这是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/
" } 

I would use a debugger and check the value of all inputs to your get_content call right before it is called using a breakpoint.我会使用调试器并在使用断点调用 get_content 调用之前检查所有输入的值。 If you don't have a debugger, add a logging function to write your vars to a log.如果您没有调试器,请添加一个日志功能以将您的变量写入日志。 You may find that one or more of them are uninitialized.您可能会发现其中一个或多个未初始化。 That would cause your error.那会导致你的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM