What is going on here?
5 posts
• Page 1 of 1
What is going on here?
Hi,
I am evaluating PHP Tools for a PHP development IDE. I have experience in C# and Delphi, and I am relatively inexperieced with PHP.
In a new PHP Console Application I added this code:
On the third line, I recieve a well deserved exception: Cannot use object of type stdClass as array
If i set a breakpoint at the third line, and use the 'immediate window' to find out more about the data structures, I get this surprising result:
So the question is: What is going on here?
I am evaluating PHP Tools for a PHP development IDE. I have experience in C# and Delphi, and I am relatively inexperieced with PHP.
In a new PHP Console Application I added this code:
- Code: Select all
$result = '{ "result": null, "message": "Application not found." }';
$json_result = json_decode($result);
echo $json_result['message'];
On the third line, I recieve a well deserved exception: Cannot use object of type stdClass as array
If i set a breakpoint at the third line, and use the 'immediate window' to find out more about the data structures, I get this surprising result:
$result
"{ \"result\": null, \"message\": \"Application not found.\" }"
$json_result
{stdClass}
$json_result['message']
"Application not found."
So the question is: What is going on here?
- Wijntjes
- Posts: 3
- Joined: February 11th, 2017, 8:17 am
Re: What is going on here?
Basically, JavaScript all works with objects. So converting JSON (JavaScript Object Notation) into PHP will, rightly so, generate objects. So, stdClass is used.
To access the "message" element, you would use object references:
Alternatively, you can ask json_decode() to return an array instead of an object:
Then your code will work as you originally expected.
As always, for help best thing is to consult the PHP docs at PHP.net.
Hope that helps.
To access the "message" element, you would use object references:
- Code: Select all
echo $json_result->message;
Alternatively, you can ask json_decode() to return an array instead of an object:
- Code: Select all
$json_result = json_decode($result, true);
Then your code will work as you originally expected.
As always, for help best thing is to consult the PHP docs at PHP.net.
Hope that helps.
-
jazz - Posts: 41
- Joined: August 25th, 2016, 1:59 am
- Location: Canberra, Australia
Re: What is going on here?
Thanks for the quick reply, but that doesn't answer my question.
The question is: Why is the 'immediate window' accepting $json_result['message']; while it generates an exception in code?
Is it using a different interpreter?
Is it less strict about the input?
In what way is it different from the php source code?
What is going on here?
The question is: Why is the 'immediate window' accepting $json_result['message']; while it generates an exception in code?
Is it using a different interpreter?
Is it less strict about the input?
In what way is it different from the php source code?
What is going on here?
- Wijntjes
- Posts: 3
- Joined: February 11th, 2017, 8:17 am
Re: What is going on here?
Ah, gotcha. Sounds like xdebug is trying to be clever and provide you a convenience. Because you are accessing the 'message' member as an array, the debugger is casting it to an array for you automatically.
Same as doing:
I did some testing and this is consistent across all classes, not just stdClass.
Same as doing:
- Code: Select all
echo ((array)$json_result)['message'];
I did some testing and this is consistent across all classes, not just stdClass.
-
jazz - Posts: 41
- Joined: August 25th, 2016, 1:59 am
- Location: Canberra, Australia
Re: What is going on here?
Thanks a lot, especially for the sample code and explanation. Now I understand what is happening.
- Wijntjes
- Posts: 3
- Joined: February 11th, 2017, 8:17 am
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 39 guests