Is there a difference between list and "Symmetric array destructuring"?

Asked

Viewed 61 times

3

I was looking for a new syntax, which was implemented in PHP 7.1, called Symmetric array destructuring, in accordance with the PHP documentation.

Basically, this allows "unstructuring" an array, much like Javascript.

Example:

[$a, $b] = ['a', 'b'];
var_dump($a, $b); 

// string(1) "a"
// string(1) "b"

However, I already know that the list I’ve been doing it for a long time.

Example:

list($a, $b) = ['a', 'b'];

var_dump($a, $b); 

// string(1) "a"
// string(1) "b"

I have some doubts about that:

  • The assignment made Symmetric array destructuring and list is the same thing with different syntax?

  • Is there any difference between the two? Performance, internal processing and the like?

  • list can become deprecated with this new implementation?

1 answer

4


PHP has the language building list(), which is used to unstructure values from within an array. That is, with list(), you can unpack values from within an array without using the index or key after the array.

Examples:

<?php
$userInfoPair = array("John Doe", 50);

list($name, $age) = $userInfoPair;

var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)

Or, if the array values are determined by keys:

<?php
$userInfo = array(
  "name" => "John Doe",
  "age" => 50
);

list("name" => $name, "age" => $age) = $userInfo;

var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)

It can then be verified that, as well as array(), the list() is not, in fact, a function, but rather a language building.

And so is the array(), which received the new bracket syntax, the list() also. Thus, I do not believe that there is a difference between the respective new syntaxes (with brackets), since they do exactly the same thing. A own documentation states that it is only a shorthand syntax as a alternative to the syntax of list().

The "problem" they must have seen to justify the introduction of these new syntaxes was to keep typing array() or list() when one wanted to build or destroy an array. Also (my guess) could easily be confused with functions, which is not technically true.

So, since nothing changes "internally", it is a mere matter of aesthetics. There is no denying that the syntax is much more succinct:

Examples (corresponding to the two above, in order):

<?php
$userInfoPair = ["John Doe", 50];

[$name, $age] = $userInfoPair;

var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)

And:

<?php
$userInfo = [
  "name" => "John Doe",
  "age" => 50
];

["name" => $name, "age" => $age] = $userInfo;

var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)

list can become deprecated with this new implementation?

It’s the same thing as asking if the building array() can become deprecated. How much legacy code depends on these constructions, think that no. However, I cannot state with complete certainty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.