Portably passing arguments to PHP's ReflectionMethod::invokeArgs by reference
Seemingly something of a misnomer, as pass by reference is deprecated in PHP 5.3... anyway, what I'm trying to do is write a unit test framework using reflection, that allows you to pass arguments to a method which requires a reference. e.g.
class Bar {
function TestMethod($arg1, &$result) {
$result = 'hello';
return true;
}
}
$rc = new ReflectionMethod('Bar', 'TestMethod');
$return_val = $rc->invokeArgs($instance, $arguments);
Now, I found a forum post at http://www.phwinfo.com/forum/comp-lang-php/273316-how-invoke-reflectionmethod-pass-variable-reference-asargument.html which clued me into simply sticking a variable reference into my $arguments array:
$arguments = array('arg1', &$byref_result);
This does work but gives a 'Deprecated: Call-time pass-by-reference has been deprecated' error. I'd like to make sure this code is forwards compatible, so if they remove it completely in PHP 6 I won't be screwed. Obviously I'd prefer not to have to rewrite any code in my project because of shortfalls in my testing framework. Any ideas or suggestions? Thanks! :)
P.S. I tried to hit up phpunit's documentation to see if it can do it but the site seems to be down for the moment. Any links to "don'开发者_StackOverflow中文版t re-invent the wheel" solutions very welcome, though I'm also curious about the question itself.
no problems with this code
class Bar {
function test($arg1, &$result) {
$result = 'hello';
}
}
$arguments = array('', &$b);
$rc = new ReflectionMethod('Bar', 'test');
$rc->invokeArgs(new Bar, $arguments);
var_dump($b);
prints "hello", no warnings
can you post your exact code?
精彩评论