PHP needs tuples!
October 20th, 2007I think the headline just about says it all.
PHP makes things dead easy by using arrays as straight arrays and hash tables, and there’s a PHP idiom to pass multiple parameters back from functions using them too. This (IMHO) would be better served with support for tuples.
An example probably helps;
Imagine i have a routine that needs to maintain backwards compatibility. It used to just return an array of values retrieved from a webservice, but now for some pages it needs to return that info (limited in scope) along with details of how many records there are in total (so they can be paged).
So old client calling code looked like
$data = $someobj->GetData();
and some newer calling pages want to return all the detail (so they pass a true in), well they have to do this
list ($data, $totalrecords) = $someobj->GetData(true);
well let’s imagine somebody does something stupid had been passing a parameter that evaluates to true (incorrectly) - after all PHP has let them because the parameter wasn’t used (and they turned warning messages off? - not that uncommon unfortunately), they don’t expect a change; it used to return an array of data… now they look at the array and there’s always two entries in there and the page is broken.
Now if PHP had tuples with a defined syntax then that could be handled nicely, and old (and bad) client calling pages/code would break (as it should - in the above circumstance it could “work” and render stuff to the page … eek!).
In Python i can say
data, numrecords = someobj.GetData(true)
here a tuple’s returned and unpacked. For the old client code if it was saying
data = someobj.GetData(true);
that now has a tuple in it.. and that’s a specific object type, so iterating it using index numbers would break.
Now, that’s a (quasi)practical example but the truth is it just makes sense for functional languages as tuples mean something different to arrays (note: i’m not saying php is a functional language so don’t flame me - it is however a scripting language with some functional attributes).
I’m just hoping zend add them at the next iteration (and take account of the rest of my moan list
)