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

Sebastian De Deyne:
The List Function & Practical Uses of Array Destructuring in PHP
May 15, 2017 @ 10:26:37

Sebastian De Deyne has written up a post to his site spotlighting PHP's list function and showing how it can be used for "array destructuring" and how recent changes in PHP 7.1.x make it more useful.

PHP 7.1 introduced a new syntax for the list() function. I've never really seen too much list() calls in the wild, but it enables you to write some pretty neat stuff.

This post is a primer of list() and it's PHP 7.1 short notation, and an overview of some use cases I've been applying them to.

He starts with a basic introduction to the list function and how it assigns out variables based on an array. He then shows examples of the updates that came with PHP 7.1, allowing you to specify the key from an array to more selectively extract only the value you want. Three "exhibits" are then provided, showing actual use cases for this functionality: basicunpacking examples, creating tuples and handling multiple return values.

tagged: list function use array destructuring php71 functionality tutorial tuple returnvalue

Link: https://sebastiandedeyne.com/posts/2017/the-list-function-and-practical-uses-of-array-destructuring-in-php

Edd Mann:
Tuples in PHP
Apr 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.

tagged: tuple data structure splfixedarray example tutorial

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