Reputation: 28
I have an object which has several nameless objects within it that I need to parse. To address this I had to cast to an array multiple times at different points while traversing it. In my localhost environment, which runs PHP7 in a windows machine, I traversed the object then casted it to an array when needed and called out the index of the variables I knew were there and then casted to an array again, all in one line.
$item = ((array)((array)$items[$x])['row'][0]);
This approach caused PHPStorm to label it as an error but worked fine in the browser. However, once I took this code to my live environment which is running PHP Version 5.3.29 on an Amazon Linux environment. I got:
[server] is currently unable to handle this request. HTTP ERROR 500
However, once I changed the code by splitting the casts into different lines it worked just fine.
$item = (array)$items[$x];
$item = (array)$item['row'][0];
My question is: Why does the first method work on my localhost environment but crashes the page once taken to a live environment? I searched the released notes of PHP7 as I though it was likely a newer feature since PHPStorm labels it as an error, but couldn't find anything that addresses this.
as a side note, I also have the following extension enabled in my localhost:
Upvotes: 0
Views: 77
Reputation: 176
As mentioned by Mark, upgrading PHP to 5.4 will allow you to use your single line example:
$item = ((array)((array)$items[$x])['row'][0]);
Upvotes: 1