What is going on here?

PHP Tools [?] extends Visual Studio with set of advanced features to work more efficiently with PHP code.

What is going on here?

Postby Wijntjes » February 11th, 2017, 8:40 am

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:

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?

Postby jazz » February 11th, 2017, 11:17 am

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:

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.
Try Hazaar MVC - http://www.hazaarmvc.com
The MVC Framework that makes PHP not suck!

Image
User avatar
jazz
 
Posts: 41
Joined: August 25th, 2016, 1:59 am
Location: Canberra, Australia

Re: What is going on here?

Postby Wijntjes » February 11th, 2017, 11:49 am

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?
Wijntjes
 
Posts: 3
Joined: February 11th, 2017, 8:17 am

Re: What is going on here?

Postby jazz » February 12th, 2017, 8:26 am

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:

Code: Select all
echo ((array)$json_result)['message'];


I did some testing and this is consistent across all classes, not just stdClass.
Try Hazaar MVC - http://www.hazaarmvc.com
The MVC Framework that makes PHP not suck!

Image
User avatar
jazz
 
Posts: 41
Joined: August 25th, 2016, 1:59 am
Location: Canberra, Australia

Re: What is going on here?

Postby Wijntjes » February 12th, 2017, 1:50 pm

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


Return to PHP Tools

Who is online

Users browsing this forum: No registered users and 42 guests

cron

User Control Panel

Login

Who is online

In total there are 42 users online :: 0 registered, 0 hidden and 42 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 42 guests