Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Fix "Fatal error: Cannot use object of type stdClass as array" errors #177

Closed
wants to merge 1 commit into from

Conversation

complex857
Copy link
Contributor

When an embed object returned which has no own GraphObject subclass the resulting GraphObject instance recieves an stdClass for backingData which is wrong since the getProperty method expects it to be an array.

Example:

$me = (new FB\FacebookRequest(
    $session, 'GET', '/me?fields=email,picture'
))->execute()->getGraphObject(FB\GraphUser::className());

// prints "false" this is clearly wrong, i asked for an array.
var_dump(is_array($me->getProperty('picture')->asArray())); 

// "Fatal error: Cannot use object of type stdClass as array"
$me->getProperty('picture')->getProperty('url'); 

The reason behind this error is this is in the __construct of GraphObject.
The code tries to detect if it got an \stdClass and do converts it to an array if it's the top level data it received, but then goes to check for a data key and there is no conversion for that value.

The proposed change make everything received from the grap api in json, to be converted into arrays instead of \stdClass regardless of nesting so you I think you can delete the explicit check and conversion too in the __construct of GraphObject

When an embed object returned which has no own GraphObject subclass the resulting GraphObject instance recieves an stdObject for backingData which is wrong since the getProperty method expects it to be an array.

Example:
    
    $me = (new FB\FacebookRequest(
        $session, 'GET', '/me?fields=email,picture'
    ))->execute()->getGraphObject(FB\GraphUser::className());
    $me->getProperty('picture')->getProperty('url'); // Fix "Fatal error: Cannot use object of type stdClass as array"
    var_dump(is_array($me->getProperty('picture')->asArray())); // prints "false"
@gfosco
Copy link
Contributor

gfosco commented Aug 1, 2014

This looks like a valid bug. nice find. But I wouldn't fix it that way. We don't want to force all json encoding to change (did you run the tests?). This should be fixed in the constructor. Do you want to add the additional fix for when 'data' is pulled?

@complex857
Copy link
Contributor Author

Sure, I'll submit a new pull request then. I'm afraid i was so excited that I've forgotten about the tests this morning.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants