News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Edd Mann:
Tuples in PHP
April 18, 2014 @ 09:48:38

Edd Mann has a new post today sharing some of his exploration into implementing tuples in PHP. A tuple is a common data structure in other languages consisting of an immutable, ordered list of items.

Since exploring languages such as Scala and Python which provide the tuple data-structure, I have been keen to experiment with how to clearly map it into a PHP solution. Tuples are simply a finite, ordered sequence of elements - usually with good language support to both pack (construction) and unpack (deconstruction) of the values. I have found that many use-cases of the common place array structure in PHP could be better suited to n-tuple's. [...] I discussed briefly that what makes tuples so powerful in the highlighted languages is their good support for handling their contents, for example unpacking a user tuple into separate id and name variables. PHP supports this form of unpacking in regard to arrays using the 'list' function, which I frequently use to return multiple values from a function/method invocation.

He shares the code for his basic implementation, extended from the SplFixedArray, and shows an example of it in use. He also includes samples showing how to make typed tuples via a "type" method call.

0 comments voice your opinion now!
tuple data structure splfixedarray example tutorial

Link: http://eddmann.com/posts/tuples-in-php/

blog comments powered by Disqus

Similar Posts

Android Hive: Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL

PHPMaster.com: Subtype Polymorphism - Swapping Implementation at Runtime

Till Klampaeckel's Blog: RFC: Mocking protected methods

IBuildings techPortal: Profiling with XHProf

ZetCode.com: PostgreSQL PHP Tutorial


Community Events





Don't see your event here?
Let us know!


series opinion introduction library version conference symfony release artisanfiles framework interview language tool list laravel voicesoftheelephpant security community podcast composer

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework