简体   繁体   中英

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:

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);

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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