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
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
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
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
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
Share on other sites

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 }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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