php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76613 Argument unpacking: Add support for associative arrays
Submitted: 2018-07-12 02:09 UTC Modified: 2018-07-12 04:46 UTC
Votes:12
Avg. Score:4.0 ± 0.9
Reproduced:12 of 12 (100.0%)
Same Version:5 (41.7%)
Same OS:5 (41.7%)
From: nowm at yandex dot ru Assigned:
Status: Suspended Package: Arrays related
PHP Version: 5.6.36 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
34 + 22 = ?
Subscribe to this entry?

 
 [2018-07-12 02:09 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-12 02:38 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2018-07-12 02:38 UTC] 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 03:00 UTC] 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 03:04 UTC] requinix@php.net
> 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.
 [2018-07-12 03:54 UTC] nowm at yandex dot ru
> so that if/when named arguments are supported, unpacking can be supported without breaking backwards compatibility

But backward compatibility already will be broken in any case, because, for example, the "call_user_func_array" function allows associative arrays. So when (or if) named arguments are supported, a lot of code will be broken. I saw a lot of code that using associative arrays in the "call_user_func_array".

<?php
// It works!
call_user_func_array('var_dump', ['a' => 1, 'b' => 'two']);
 [2018-07-12 04:46 UTC] requinix@php.net
Whether or not such code would be broken is a matter for the implementation of named arguments, but at a minimum has nothing to do with array unpacking.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC