PHP syntax for dereferencing function result
This is specifically array dereferencing, which is currently unsupported in php5.3 but should be possible in the next release, 5.4. Object dereferencing is on the other hand possible in current php releases. I'm also looking forward to this functionality!
PHP 5.3 accessing array key from object getter
Array dereferencing as described in the question is a feature that was only added in PHP 5.4. PHP 5.3 cannot do this.
echo $form->fields()['fieldname']
So this code will work in PHP 5.4 and higher.
In order to make this work in PHP 5.3, you need to do one of the following:
Use a temporary variable:
$temp = $form->fields()
echo $temp['fieldname'];Output the fields array as an object property rather than from a method:
ie this....echo $form->fields['fieldname']
...is perfectly valid.
Or, of course, you could upgrade your server to PHP 5.4. Bear in mind that 5.3 will be declared end-of-life relatively soon, now that 5.5 has been released, so you'll be wanting to upgrade sooner or later anyway; maybe this is your cue to do? (and don't worry about it; the upgrade path from 5.3 to 5.4 is pretty easy; there's nothing really that will break, except things that were deprecated anyway)
Why does this work? PHP Direct array value from function
It's called array dereferencing, you can read about it here:
Function array dereferencing has been added, e.g.
foo()[0]
.
Another bit of information here:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
PHP Version 5.3 vs Version 5.6 and above
The short array syntax was introduced in 5.4. But you misunderstood. This supports []
instead of array()
syntax for literal declarations.
However, the syntax
$array[$index][] = $someVar;
has nothing to do with short array syntax. It's been valid since the beginning of PHP. You can use this syntax in older version, too.
When the code is changed to
$sortedMainCatArray[$letter] = array($eachMainCategory);
it basically says that do not create a sub-array at $letter index of the main array (multiple calls to this line results in multiple elements in the subarray) but assign array($eachMainCategory)
to that index (multiple calls to this line assign the same array to that index multiple times). To sum up, leaving
$sortedMainCatArray[$letter][] = $eachMainCategory;
for both PHP versions is OK.
UPDATE
Accessing an array element at a specific index when that array is returned by a function is called array dereferencing support
and was also added in 5.4, so the one liner allowed by it functionThatReturnsArray()[$index]
can only be done in two expressions (lines) in PHP prior to 5.4 as shown in the PHP manual examples section:
// on PHP 5.4
$secondElement = getArray()[1];
// previously
$tmp = getArray();
$secondElement = $tmp[1];
Weird behaviour when using PHP mysqli row
The ability to reference the elements of a returned array directly from the calling method is introduced in PHP 5.4 - which is why it's not working in 5.3.
From Example #7 on http://php.net/manual/en/language.types.array.php
As of PHP 5.4 it is possible to array dereference the result of a
function or method call directly. Before it was only possible using a
temporary variable.
So your temporary solution looks like it will become a long-term one :)
Related Topics
PHP Sessions Login with Remember Me
PHP Regex to Remove Http:// from String
How to Design a Hierarchical Role Based Access Control System
Understanding Doctrine Cascade Operations
PHP Preg_Replace/Preg_Match VS PHP Str_Replace
A Restful Persistence Solution Usable with Backbone.Js... in PHP
Using Websocket on Apache Server
Location for Session Files in Apache/Php
Get String Between - Find All Occurrences PHP
Google-Like Search Engine in PHP/Mysql
PHP - Iterate on String Characters
Jquery Tablesorter Index Column Insert
How to Write to the Console from a Laravel Controller
Send Post Data via Raw JSON with Postman
Php_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known