Jump to content

stdClass objects


monkeytooth

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/241046-stdclass-objects/
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238161
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238299
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238304
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/241046-stdclass-objects/#findComment-1238305
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.