Accessing data in an object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myquealer
Forum Newbie
Posts: 3
Joined: Thu Mar 05, 2009 5:04 pm

Accessing data in an object

Post by myquealer »

I am fairly well versed in PHP, but don't have all that much experience with objects. I am using a class for Twitter that I found and when requesting tweets for a particular user it returns an object named $myObject with the following structure:

Code: Select all

stdClass Object
(
    [0] => stdClass Object
        (
            [text] => Signing In Back to Normal http://tinyurl.com/agklal
            [user] => stdClass Object
                (
                    [description] => Always wondering what everyone's doing.
                    [profile_image_url] => http://s3.amazonaws.com/twitter_product ... normal.png
                    [url] => http://twitter.com
                    [screen_name] => twitter
                    [name] => Twitter
                    [protected] => 
                    [followers_count] => 313718
                    [location] => San Francisco, CA
                    [id] => 783214
                )
 
            [created_at] => Thu Mar 05 00:48:31 +0000 2009
            [favorited] => 
            [in_reply_to_user_id] => 
            [truncated] => 
            [id] => 1281186929
            [in_reply_to_screen_name] => 
            [in_reply_to_status_id] => 
            [source] => twitterfeed
        )
 
I cannot figure out how to access the values in this object. $myObject[0]['text'] does not work, $myObject->0['text'] does not work, $myObject->0->text does not work, foreach($myObject as $tweet) does not work, foreach($myObject as $key=>$tweet) does not work. Nothing I've tried works. What am I missing?

The object is like an array of objects, I only included the first element here, but there is "[1] => stdClass Object" and so on after this one.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Accessing data in an object

Post by Mark Baker »

An object isn't an array, so you don't use techniques such $myObject['text'] to access it.

Instead, you'd use $myObject->text to access the "text" property of the instantiated object, assuming that the "text" property wasn't private, or $myObject->user->description to access the instead of $myObject['user']['description'].
myquealer
Forum Newbie
Posts: 3
Joined: Thu Mar 05, 2009 5:04 pm

Re: Accessing data in an object

Post by myquealer »

Thanks for responding.

I realize an object is not an array, I have tried to access it in the objectish ways I know of as well. $myObject->text doesn't work because the main object contains something like an indexed array of objects. 'text' is a child of 0 which is a child of $myObject, so I tried $myObject->0->text, but that does not work. I tried all sorts of other things too, which also fail.
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Accessing data in an object

Post by BomBas »

Can't you change the 0 to something else? I think you should try it.
myquealer
Forum Newbie
Posts: 3
Joined: Thu Mar 05, 2009 5:04 pm

Re: Accessing data in an object

Post by myquealer »

The 0 is being created by the Twitter library I am using, and that is using the PHP function json_decode to convert Twitter's JSON response into an object, so really the 0 is being created by PHP's json_decode function. I figure if PHP makes this object there must be some way to access it.
Post Reply