Req #76613 [Sus]: Argument unpacking: Add support for associative arrays
Edit report at https://bugs.php.net/bug.php?id=76613&edit=1
ID: 76613
Updated by: requinix@php.net
Reported by: nowm at yandex dot ru
Summary: Argument unpacking: Add support for associative
arrays
Status: Suspended
Type: Feature/Change Request
Package: Arrays related
Operating System: any
PHP Version: 5.6.36
Block user comment: N
Private report: N
New Comment:
> an associative array is the ideal fit for named arguments unpacking, am I wrong?
You're right, it would be good for named arguments. Which is why the current version of
unpacking specifically does not allow it, so that if/when named arguments are supported, unpacking
can be supported without breaking backwards compatibility.
Previous Comments:
------------------------------------------------------------------------
[2018-07-12 03:00:09] nowm at yandex dot ru
requinix, but an associative array is the ideal fit for named arguments unpacking, am I wrong?
<?php
$string = 'some string';
htmlspecialchars(...['string' => $string, 'double_encode' => false]);
Anyway, thank you for the review, even if the request was suspended.
------------------------------------------------------------------------
[2018-07-12 02:38:07] requinix@php.net
> In order to ensure forward-compatibility with named arguments the unpacking operator does not
> support string keys.
That particular RFC is no longer active but the feature as a whole still has its proponents.
------------------------------------------------------------------------
[2018-07-12 02:09:00] nowm at yandex dot ru
Description:
------------
This request is related to any PHP 5.6+
When you pass an associative array for argument unpacking
(https://wiki.php.net/rfc/argument_unpacking), you get a fatal error, "Cannot unpack array with
string keys".
As I see, there are no reasons to limit unpacking to only numeric arrays. For example, it does not
use numeric keys for ordering values, so key type has no any sense:
<?php
$array = [1 => 'b', 2 => 'c', 0 => 'a'];
// Outputs "b c a", not "a b c"
// It shows that array unpacking does not rely on keys at all
var_dump(...$array);
Test script:
---------------
<?php
$array = ['a' => 'one', 'b' => 'two', 'c'
=> 'three'];
var_dump(...$array);
Expected result:
----------------
string(3) "one"
string(3) "two"
string(5) "three"
Actual result:
--------------
PHP Fatal error: Uncaught Error: Cannot unpack array with string keys in Standard input code:5
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76613&edit=1
Thread (6 messages)