PHP supports the list()
language construct which, in short, allows you to return multiple values from a function and then attach them to different variable, eg:
function myBigReturn(){
return array("foo", "bar");
}
list($fooer, $barer) = myBigReturn();
echo $fooer; // echoes "foo"
echo $barer; // echoes "bar"
I have failed to find much info about such a language construct and I am curious – is usage of PHP list()
construct considered a bad coding convention? Are there any serious articles/literature on this subject?
4
I dare say it is almost pythonic. There is a similar construct in Python and very useful for automatically splitting out the return values.
The place I have seen it the most is when a function returns a point, which has an x/y, latitude/longitude, or azimuth and range value. At times a function can return a object with defined fields, but sometimes the automatic splitting is just what is needed.
As Rath points out there is no real speed difference.
It is generally not used when the number of items in the array is more than a handful, or if there is not a fixed number of items being returned.
4