簡體   English   中英

PHP 中的錯誤:“試圖獲取非對象的屬性”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM