php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80991 There is no way to revoke reflection method with by-reference args
Submitted: 2021-04-26 14:58 UTC Modified: 2021-04-27 12:03 UTC
From: g dot sokol99 at g-sokol dot info Assigned: cmb (profile)
Status: Not a bug Package: Reflection related
PHP Version: 7.4.16 OS: linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: g dot sokol99 at g-sokol dot info
New email:
PHP Version: OS:

 

 [2021-04-26 14:58 UTC] g dot sokol99 at g-sokol dot info
Description:
------------
If we have some method that receives some arguments my reference, there is no way to invoke it with reflection.

Test script:
---------------
class TestReflectionMethodInvokeWithReferences
{
    private static function testTarget(array &$a): void
    {
        $a['new_key'] = 'new value';
    }
}

// Unit test use case
$testReflectionMethodInvokeWithReferences = new ReflectionClass(TestReflectionMethodInvokeWithReferences::class);
$target = $testReflectionMethodInvokeWithReferences->getMethod('testTarget');
$target->setAccessible(true);
$arg = [];
$target->invoke(null, &$arg);
var_exoprt($arg);

Expected result:
----------------
array (
  'new_key' => 'new value',
)


Actual result:
--------------
PHP Parse error:  syntax error, unexpected token "&", expecting ")" in php shell code on line 5

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-26 15:01 UTC] g dot sokol99 at g-sokol dot info
Oh, there is workaround:
```
$target->invokeArgs(null, [&$arg]);
```
 [2021-04-26 15:02 UTC] g dot sokol99 at g-sokol dot info
-Status: Open +Status: Closed
 [2021-04-26 15:02 UTC] g dot sokol99 at g-sokol dot info
Oh, there is workaround:
```
$target->invokeArgs(null, [&$arg]);
```
 [2021-04-26 15:50 UTC] cmb@php.net
-Status: Closed +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-04-26 15:50 UTC] cmb@php.net
Right, you can use ::invokeArgs() instead.  The documentation of
::invoke() refers to call_user_func(), and there this issue is
documented for the $args parameter[1].

[1] <https://www.php.net/manual/en/function.call-user-func.php#refsect1-function.call-user-func-parameters>
 [2021-04-27 12:03 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-04-27 12:03 UTC] cmb@php.net
Re-opening as doc problem.
 [2021-04-27 12:03 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Add note for passing references to invoke methods.
On GitHub:  https://github.com/php/doc-en/pull/550
Patch:      https://github.com/php/doc-en/pull/550.patch
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC