Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
2  
Why would it be a "bad coding convention"? Just because a function, technique, concept doesn't exist on another language, it doesn't mean there's something wrong with it. Also: PHP list() equivalent in Python. –  Yannis Rizos Jul 23 '13 at 11:34
    
@YannisRizos I am not saying it's a bad convention, I am simply asking. The question stems from the fact that I want to leave a manageable code behind me, and I am curious if list() is widely accepted as good or bad code and thus can affect the readability for further generations of programmers. –  Maurycy Zarzycki Jul 23 '13 at 12:41
    
Every structure / technique / practice can become a bad convention, if sufficiently abused. If you use it for a handful of values, as mhoran_psprep suggests, you shouldn't have any problems. –  Yannis Rizos Jul 23 '13 at 13:33
    
Another option for handling multiple return values is to return an instance of stdClass, with the multiple values you want to send back set as properties in that object. A good example of that is fetchObject() in PDO. –  GrandmasterB Jul 23 '13 at 17:42

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Also good for exploding dates: list($year, $month, $day) = explode('-', $isodate); or the matches of preg_match: if (preg_match('/(.).(.)/', $text, $matches)) { list (, $first, $second) = $matches; } (notice the first comma to avoid $matches[0]. As list is a language construct and not a function, you can do that). –  Carlos Campderrós Jul 23 '13 at 13:50
    
Well, even if "pythonic" makes sense applied to things which aren't Python, it isn't necessarily good in the context of the whole design. I say this despite liking Python very much and PHP not so much. –  delnan Jul 23 '13 at 15:01
    
Mind: x/y, latitude/longitude, ... in PHP will typically use associative arrays (dictionaries in other environments). With code like function getCoords() { return ['long' => 48.13, 'lat' => 11.57];} list($long, $lat) = getCoords(); the caller depends on implementation details - the order in which the elements were added, not the names. –  johannes Jul 23 '13 at 15:54
    
It's not pythonic...I suspect its more Perlonic, as such behavior is available in Perl, and early PHP was largely derived from Perl. –  GrandmasterB Jul 23 '13 at 17:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.