stdClass issue with detecting isset, empty, other
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..
You don't say, but this sounds like the error is given for the lines like
$fbHomeTown = $fb_result->hometown->name;
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:
$fbHomeTown = isset($fb_result->hometown->name) ? trim($fb_result->hometown->name) : null;
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 doing
if($fbHomeTown === null) ... // 3 equal signs
However, 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
echo "<strong>First Name: </strong>".
(empty($fbFirstName) ? "Not found.<br />" : $fbFirstName);
精彩评论