stdClass 检测 isset、empty、other 时出现问题
首先我要说的是我正在使用 CodeIgniter 和 Elliot Haughin 创建的 Facebook 类。 http://www.haughin.com/code/facebook/ 我有工作发现一切都很好。然而,当我使用各种 FB 帐户测试我的代码时,我偶然发现了一个我想评估的问题,但我的尝试失败了。我用谷歌搜索并访问了其他论坛,并且不断得出我最初想到的答案。无论如何应该起作用的东西,但在这种情况下似乎不起作用..
既然我已经说过了,我的问题是.. FB 传回的对象并不总是有一个特定的条目。为了这个例子,我会说用户名..
这是一个使用用户名返回的数据的示例:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 0000000000000
[name] => Random.User
[first_name] => Random
[middle_name] => ...
[last_name] => User
[link] => http://www.facebook.com/profile.php?id=000000000
[birthday] => 11/26/1980
[gender] => male
[timezone] => -4
[locale] => en_US
[verified] => 1
[updated_time] => 2011-05-30T19:24:53+0000
)
这是一个没有用户名的示例:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 000000000000000000000
[name] => Random User
[first_name] => Random
[last_name] => User
[link] => http://www.facebook.com/Random.User
[username] => Random.User
[birthday] => 11/10/1985
[hometown] => stdClass Object
(
[id] => 000000000000000
[name] => Coventry, Connecticut
)
[location] => stdClass Object
(
[id] => 00000000000000
[name] => San Jose, California
)
我上次尝试解决问题失败的尝试类似于..
$fb_result = $this->facebook->call('get', 'me', array('metadata' => 1));
$fbFirstName = $fb_result->first_name;
$fbLastName = $fb_result->last_name;
$fbUserName = $fb_result->username;
$fbUserId = $fb_result->id;
$fbHomeTown = $fb_result->hometown->name;
$fbLocation = $fb_result->location->name;
echo "<strong>First Name: </strong>"; if((isset($fbFirstName))AND(!empty($fbFirstName))AND(trim($fbFirstName) !== "")){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Last Name: </strong>"; if((isset($fbLastName))AND(!empty($fbLastName))AND(trim($fbLastName) !== "")){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Username: </strong>"; if((isset($fbUserName))AND(!empty($fbUserName))AND(trim($fbUserName) !== "")){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>User FB ID: </strong>"; if((isset($fbUserId))AND(!empty($fbUserId))AND(trim($fbUserId) !== "")){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location hometown: </strong>"; if((isset($fbHomeTown))AND(!empty($fbHomeTown))AND(trim($fbHomeTown) !== "")){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location current (manual input): </strong>"; if((isset($fbLocation))AND(!empty($fbLocation))AND(trim($fbLocation) !== "")){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; }
它在哪里做我首先将其定义为变量,然后使用 if-else 检查它,但是现在是否有效。我现在收到“尝试获取非对象的属性”错误。这意味着我想因为它不存在并且我试图将它设置为变量,无论它会返回一个错误。我昨天和昨天花了大部分时间试图解决这个问题,但我不得不说我陷入了困境。 php错误类型只不过是一个“通知”,但我通常不喜欢让我的代码处于这样的状态,即使是像这样的错误也会踢回来..这就是说我迷路了..
First off let me start by saying I am using CodeIgniter and the Facebook Class Elliot Haughin created. http://www.haughin.com/code/facebook/ Which I have that working find all all is well. However when I was testing my code with various FB accounts I stumbled across an issue I'd like to assess but my attempts have failed. I have googled around and hit up other forums and I consistently come up with the answers I originally came up with. Something that should by all means work but doesn't seem to in this case..
Now that I have said that, my issue is.. The object that FB is passing back doesn't always have a particular entry in it. For the sake of this example Ill say the username..
Heres an example of the Data returned with a username:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 0000000000000
[name] => Random.User
[first_name] => Random
[middle_name] => ...
[last_name] => User
[link] => http://www.facebook.com/profile.php?id=000000000
[birthday] => 11/26/1980
[gender] => male
[timezone] => -4
[locale] => en_US
[verified] => 1
[updated_time] => 2011-05-30T19:24:53+0000
)
Here is an example without a username:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 000000000000000000000
[name] => Random User
[first_name] => Random
[last_name] => User
[link] => http://www.facebook.com/Random.User
[username] => Random.User
[birthday] => 11/10/1985
[hometown] => stdClass Object
(
[id] => 000000000000000
[name] => Coventry, Connecticut
)
[location] => stdClass Object
(
[id] => 00000000000000
[name] => San Jose, California
)
my last failed attempt at trying to trouble shoot this goes something like..
$fb_result = $this->facebook->call('get', 'me', array('metadata' => 1));
$fbFirstName = $fb_result->first_name;
$fbLastName = $fb_result->last_name;
$fbUserName = $fb_result->username;
$fbUserId = $fb_result->id;
$fbHomeTown = $fb_result->hometown->name;
$fbLocation = $fb_result->location->name;
echo "<strong>First Name: </strong>"; if((isset($fbFirstName))AND(!empty($fbFirstName))AND(trim($fbFirstName) !== "")){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Last Name: </strong>"; if((isset($fbLastName))AND(!empty($fbLastName))AND(trim($fbLastName) !== "")){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Username: </strong>"; if((isset($fbUserName))AND(!empty($fbUserName))AND(trim($fbUserName) !== "")){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>User FB ID: </strong>"; if((isset($fbUserId))AND(!empty($fbUserId))AND(trim($fbUserId) !== "")){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location hometown: </strong>"; if((isset($fbHomeTown))AND(!empty($fbHomeTown))AND(trim($fbHomeTown) !== "")){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location current (manual input): </strong>"; if((isset($fbLocation))AND(!empty($fbLocation))AND(trim($fbLocation) !== "")){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; }
Which doing it where I am defining it as a variable first then checking it with if-else now is working however. I am now getting "Trying to get property of non-object" for an error. Which means I suppose that since its not there and I am trying to set it as a variable regardless it kicks back an error for it. I have spent the better part of the day and yesterday trying to resolve this and I have to say I am stuck. The php error type is nothing more then a mere "Notice" but I don't generally like to leave my code in a status that will kick even so much as an error like that back.. That said I am lost..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有说,但这听起来像是针对
诸如
hometown
属性不存在等行给出的错误。防止出现此通知的方法(并且您绝对应该争取完全无错误的代码)是执行以下操作:
这将您的临时变量设置为已知的安全值(修剪后的值或 null )。然后,您可以通过执行以下操作来显式检查没有价值的情况,
但是,在很多情况下(包括您的情况),您甚至不需要区分“不存在”和“它是一个空字符串”和您将能够轻松完成处理,例如
You don't say, but this sounds like the error is given for the lines like
when e.g. the
hometown
property does not exist.The way to prevent this notice (and you should definitely strive for completely error-free code) is to do something like this:
This sets your temporary variable to a known safe value (either the trimmed value or
null
). You can then check explicitly for the case where there was no value to begin with by doingHowever, in lots of cases (including yours) you would not even need to distinguish between the "did not exist" and "it's an empty string" and you would be able to easily complete the processing with something like