monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 Ok, I am working on an app for FB. Connecting to FB and using the Graph API they provide I have stdClass objects sent back to my script. I generally don't control the information they send so that said. The object they send can either contain or not contain a particular object. Example in this case.. Someones User Name (they didn't set up a easily memorable fb.com/user.name). One user if they have it set the class with contain an object username however if they didn't set it then the object wont be part of the class. So What I am trying to figure out is how do I check to see if an object in the class is there or not? currently a simple echo $fb_result->username; is all I need to have it show the username on the page. but if its not in the class I get Undefined property: stdClass::$username Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/ Share on other sites More sharing options...
Alex Posted July 4, 2011 Share Posted July 4, 2011 Use isset. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238134 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Author Share Posted July 4, 2011 if(isset($fb_result->username)) Hmm think I tried that and still failed.. but maybe not.. I don't remember now.. but ill try it again anyway :-) Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238146 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Author Share Posted July 4, 2011 I was right I did fail with just isset, I think I ended up playing to much with it, then forgot where I left off when I came back to it.. Anyway the point is for sake of example if I do this.. if(isset($fb_result->first_name)){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; } echo $fb_result->first_name; The if statement will echo out the "Not Found." but yet the next echo will output the right thing.. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238161 Share on other sites More sharing options...
Alex Posted July 4, 2011 Share Posted July 4, 2011 Can you post an example structure of $fb_result and the exact code you're using to test? Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238180 Share on other sites More sharing options...
trq Posted July 4, 2011 Share Posted July 4, 2011 You could always check property_exists. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238186 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Author Share Posted July 4, 2011 Example of username not in class object facebookResponse Object ( [__construct:private] => [__resp] => stdClass Object ( [data] => stdClass Object ( [id] => 100001593440934 [name] => Tristan Skylar Francisco Hacia [first_name] => Tristan [middle_name] => Skylar Francisco [last_name] => Hacia [link] => http://www.facebook.com/profile.php?id=100001593440934 [birthday] => 11/26/1980 [gender] => male [timezone] => -4 [locale] => en_US [verified] => 1 [updated_time] => 2011-05-30T19:24:53+0000 ) Example with username in object: facebookResponse Object ( [__construct:private] => [__resp] => stdClass Object ( [data] => stdClass Object ( [id] => 100001396941300 [name] => Christopher Hacia [first_name] => Christopher [last_name] => Hacia [link] => http://www.facebook.com/christopher.hacia [username] => christopher.hacia [birthday] => 11/10/1980 [hometown] => stdClass Object ( [id] => 104131819622020 [name] => Coventry, Connecticut ) [location] => stdClass Object ( [id] => 111948542155151 [name] => San Jose, California ) Example of last failed attempt to see if it is or is not in the object: $fb_result = $this->facebook->call('get', 'me', array('metadata' => 1)); echo "<strong>First Name: </strong>"; if(isset($fb_result->first_name)){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; } echo "<strong>Last Name: </strong>"; if(isset($fb_result->last_name)){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; } echo "<strong>Username: </strong>"; if(isset($fb_result->username)){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; } echo "<strong>User FB ID: </strong>"; if(isset($fb_result->id)){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; } echo "<strong>Location hometown: </strong>"; if(isset($fb_result->hometown->name)){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; } echo "<strong>Location current (manual input): </strong>"; if(isset($fb_result->location->name)){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; } echo $fb_result->first_name output of last attempt: First Name: Not found. Last Name: Not found. Username: Not found. User FB ID: Not found. Location hometown: Coventry, Connecticut Location current (manual input): San Jose, California Christopher Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238299 Share on other sites More sharing options...
Alex Posted July 4, 2011 Share Posted July 4, 2011 Um.. Then it looks like the reason is because you're not directly accessing the properties. It looks like the facebook response object is making use of __get, maybe. I'm not familiar with the object, so there's probably a better way to do this (a built in method), but this should work: $fb_result->__resp->data->first_name; instead of just $fb_result->first_name; Then you can use that in isset. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238304 Share on other sites More sharing options...
xyph Posted July 4, 2011 Share Posted July 4, 2011 Odd that hometown and location objects are working. Though, I think the isset call is redundant. Assuming it's the right object, it HAS to have that property, defined or not. It's odd it's returning FALSE though. Perhaps check if it's !empty() instead? Edit - I was thinking the same thing too, AlexWD, but I figured $fb_result was the [data] object. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238305 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Author Share Posted July 4, 2011 Its funny, I change it to !empty() and firstname - id come back not found, while hometown and location come up.. I change them all to just empty() and firstname - id come up, while hometown and location return not found.. Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238309 Share on other sites More sharing options...
xyph Posted July 4, 2011 Share Posted July 4, 2011 AlexWD is probably correct. There's some magic __get() function that's screwing up your checks. Try using his solution. It's redundant, but try something like this, just to debug. $first_name = $fb_result->first_name; if( !empty($first_name) ) { etc } Quote Link to comment https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.