Req #76613 [Com]: Argument unpacking: Add support for associative arrays

From: Date: Thu, 12 Jul 2018 03:54:13 +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-216286@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:

> 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']);


Previous Comments:
------------------------------------------------------------------------
[2018-07-12 03:04:53] 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: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)

« previous php.bugs (#216286) next »