php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60140 ReflectionMethod::invoke*() does not work for pass-by-ref arguments
Submitted: 2011-10-26 11:05 UTC Modified: 2011-10-27 15:39 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sebastian@php.net Assigned:
Status: Wont fix Package: Reflection related
PHP Version: 5.3SVN-2011-10-26 (SVN) OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-10-26 11:05 UTC] sebastian@php.net
Description:
------------
ReflectionMethod::invoke*() does not work for methods that expect pass-by-reference arguments.

Test script:
---------------
<?php
class Foo
{
    public function bar(&$baz)
    {
    }
}

$array  = array();
$object = new Foo;

$method = new ReflectionMethod('Foo', 'bar');
$method->invoke($object, $array);
?>

Actual result:
--------------
Warning: Parameter 1 to Foo::bar() expected to be a reference, value given in /home/sb/test.php on line 13

Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method Foo::bar() failed' in /home/sb/test.php:13
Stack trace:
#0 /home/sb/test.php(13): ReflectionMethod->invoke(Object(Foo), Array)
#1 {main}
  thrown in /home/sb/test.php on line 13


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-27 10:59 UTC] cataphract@php.net
-Type: Bug +Type: Documentation Problem
 [2011-10-27 10:59 UTC] cataphract@php.net
Not a bug, it should probably be documented that invoke doesn't support calling the underlying method with by-ref arguments and invokeArgs should be used instead.
 [2011-10-27 11:01 UTC] sebastian@php.net
invokeArgs() works:

<?php
class Foo
{
    public function bar(&$baz)
    {
    }
}

$array  = array();
$object = new Foo;

$method = new ReflectionMethod('Foo', 'bar');
$method->invokeArgs($object, array(&$array));
?>
 [2011-10-27 15:39 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2011-10-27 15:39 UTC] laruence@php.net
<laruence> a work fix for bug60140(invoke, reference) http://pastebin.com/ZYc4yy8P
<laruence> but not sure,  does it can cause some side-effect. 
<laruence> however,  the ext/reflection tests all passed
<johannes_> i doubt it is correct
<johannes_> you're setting the isref flag but you might have a copy
<johannes_> so do   function test(&$param) { $param = 23, } $r = new Reflectionfunction("test"); $a = 
42; $b = &$a; $r->invoke($a); 
<johannes_> and then the result is wrong
<johannes_> the way to fix it is to take all paramters by-ref by default and then separate them if 
needed
<johannes_> which has many many many other issues
<laruence> en , yes,  if that way,  $ref->invoke("value") will be broken
<johannes_> thanks for trying to fix it (it's an old issue, and it's good to have "new" people 
thinking about it, sometimes there are new solutions, sometimes these might work)
 [2013-10-08 22:59 UTC] spekary at gmail dot com
The status of this is "Wont Fix", which was fine when this was a regular bug, but when changed to a doc bug, the status should have been changed to high priority. This "problem" was a change in behavior to PHP sometime around version 5.2-5.3, and debugging it can be quite a bear. 

The Note on the documentation page says that you can pass references to this function, but that is definitely not true. Please change the note to read something like:

Note: You cannot pass references to functions using "invoke". Use invokeArgs instead if you need to pass referenced variables.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 16:01:29 2024 UTC