Req #76613 [Com]: Argument unpacking: Add support for associative arrays
| From: | nowm at yandex dot ru | Date: | Thu, 12 Jul 2018 03:00:10 +0000 |
| Subject: | Req #76613 [Com]: Argument unpacking: Add support for associative arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-216283@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76613&edit=1
ID: 76613
Comment by: nowm at yandex dot ru
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:
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.
Previous Comments:
------------------------------------------------------------------------
[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