Doc #60140 [Com]: ReflectionMethod::invoke*() does not work for pass-by-ref arguments
| From: | spekary at gmail dot com | Date: | Tue, 08 Oct 2013 22:59:41 +0000 |
| Subject: | Doc #60140 [Com]: ReflectionMethod::invoke*() does not work for pass-by-ref arguments | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10444@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60140&edit=1
ID: 60140
Comment by: spekary at gmail dot com
Reported by: sebastian@php.net
Summary: ReflectionMethod::invoke*() does not work for
pass-by-ref arguments
Status: Wont fix
Type: Documentation Problem
Package: Reflection related
Operating System: Irrelevant
PHP Version: 5.3SVN-2011-10-26 (SVN)
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-10-27 15:39:22] 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)
------------------------------------------------------------------------
[2011-10-27 11:01:11] 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 10:59:11] 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-26 11:05:55] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60140&edit=1