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.
我有以下代码,它可以正常工作。 Twitter好友列出正确,但似乎当显示最后一项时,错误“通知:试图获取非对象的属性”显示4次。
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 />";
}
}
5 个解决方案
#1
you can add a checker if the object has a certain property before using its value
如果对象在使用其值之前具有某个属性,则可以添加检查器
if (isset($tweet[$i]->name)) {
// process
}
#2
Use a simple if condition before print..
打印前使用简单的if条件..
$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 />";
}
}
}
#3
replace this :
替换这个:
for($i = 0; $i < count($tweet); $i++)
with this :
有了这个 :
for($i = 0; $i < count($tweet) - 1; $i++)
EDIT
for($i = 0; $i < count($tweet); $i++){
if (isset($tweet[$i]->name)) {
echo $tweet[$i] -> name;
echo "<br />";
}
}
try this
#4
use if empty for prevent that notice.
如果为空则使用以防止该通知。
$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(empty($tweet[$i]->name)) continue;
echo $tweet[$i]->name;
echo "<br />";
}
}
#5
While the accepted answer will work, PHP has the property_exists()
function to do this job and would be more appropriate. It will return true even if the property has a null value, which isset()
does not.
虽然接受的答案可行,但PHP有property_exists()函数来完成这项工作,并且更合适。即使属性具有空值,它也将返回true,而isset()则不是。
if (property_exists($tweets[$i], "name")) {
....
}
#1
you can add a checker if the object has a certain property before using its value
如果对象在使用其值之前具有某个属性,则可以添加检查器
if (isset($tweet[$i]->name)) {
// process
}
#2
Use a simple if condition before print..
打印前使用简单的if条件..
$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 />";
}
}
}
#3
replace this :
替换这个:
for($i = 0; $i < count($tweet); $i++)
with this :
有了这个 :
for($i = 0; $i < count($tweet) - 1; $i++)
EDIT
for($i = 0; $i < count($tweet); $i++){
if (isset($tweet[$i]->name)) {
echo $tweet[$i] -> name;
echo "<br />";
}
}
try this
#4
use if empty for prevent that notice.
如果为空则使用以防止该通知。
$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(empty($tweet[$i]->name)) continue;
echo $tweet[$i]->name;
echo "<br />";
}
}
#5
While the accepted answer will work, PHP has the property_exists()
function to do this job and would be more appropriate. It will return true even if the property has a null value, which isset()
does not.
虽然接受的答案可行,但PHP有property_exists()函数来完成这项工作,并且更合适。即使属性具有空值,它也将返回true,而isset()则不是。
if (property_exists($tweets[$i], "name")) {
....
}