Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide error "Trying to get property of non-object"

I have the following code, which works correctly. The Twitter Friends are listed correctly, however it seems that when the last item is displayed the error "Notice: Trying to get property of non-object" is displayed 4 times.

Since the code works as it should, I would like a way to hide these errors.

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
    for($i = 0; $i < count($tweet); $i++)
    {
        echo $tweet[$i] -> name;
        echo "<br />";
    }
}
like image 350
user2675041 Avatar asked Feb 10 '23 06:02

user2675041


2 Answers

you can add a checker if the object has a certain property before using its value

if (isset($tweet[$i]->name)) {
    // process

}
like image 150
Oli Soproni B. Avatar answered Feb 13 '23 03:02

Oli Soproni B.


Use a simple if condition before print..

   $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
   $tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
   for($i = 0; $i < count($tweet); $i++){  
     if($tweet[$i]){
        echo $tweet[$i] -> name;
        echo "<br />";
     }
   }
}
like image 23
Sathya Baman Avatar answered Feb 13 '23 03:02

Sathya Baman