Doc #80531 [Com]: call_user_func_array() interprets string keys as named args

[email protected] ("barryuyuamo at gmail dot com") Fri, 02 Dec 2022 06:29:16 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=80531&edit=1

 ID:                 80531
 Comment by:         barryuyuamo at gmail dot com
 Reported by:        rh at tfli dot co dot uk
 Summary:            call_user_func_array() interprets string keys as
                     named args
 Status:             Open
 Type:               Documentation Problem
 Package:            Scripting Engine problem
 Operating System:   Windows 10 (64Bit)
 PHP Version:        8.0.0
 Block user comment: N
 Private report:     N

 New Comment:

I'm especially stayed aware of the article and I will get many benefits from it. Subsequently, thank you for sharing it.
 (https://www.allieduniversalehub.net/)github.com


Previous Comments:
------------------------------------------------------------------------
[2020-12-18 09:50:35] rh at tfli dot co dot uk

Ah, right you are! I did do a search for `named` (in relation to named arguments) before reporting the bug; and I did read through the full guide when upgrading.

------------------------------------------------------------------------
[2020-12-18 09:46:32] [email protected]

This is documented in the migration guide (https://www.php.net/manual/en/migration80.incompatible.php):

> call_user_func_array() array keys will now be interpreted as parameter names, instead of being silently ignored.

However, it is not yet mentioned in the call_user_func_array() documentation itself.

------------------------------------------------------------------------
[2020-12-18 09:43:02] rh at tfli dot co dot uk

Description:
------------
This might be expected behaviour, but if so, should be made clear in the migration guide and/or the function page.

Since upgrading to PHP8, then an unchanged call to call_user_func_array in an existing project was returning the error 'Uncaught Error: Cannot use positional argument after named argument'. Investigating it further, one of the arguments is de-serialised from a JSON object (and so the array has string keys).

While I can see the behaviour of `call_user_func_array` using string keys as named arguments being useful, I can also see it being a massive 'gotta' for people when upgrading, especially as it is not currently documented anywhere that this will be the case (a lot of the times this can happen the array internal structure won't be apparent from the code or the error message).

Test script:
---------------
<?php
var_dump(call_user_func_array("strlen", ["hello"]));
var_dump(call_user_func_array("strlen", ["a" => "hello"]));

// Version Comparison: https://3v4l.org/c1R03

Expected result:
----------------
int(5)
int(5)

Actual result:
--------------
int(5)

Fatal error: Uncaught Error: Unknown named parameter $a in /in/c1R03:3
Stack trace:
#0 {main}
  thrown in /in/c1R03 on line 3

Process exited with code 255.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80531&edit=1