views:

165

answers:

1

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't re-invent the wheel" solutions very welcome, though I'm also curious about the question itself.

A: 

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?

stereofrog
Hi,It turns out the issue PHP had was with me using array_push with my result variable passed by reference - I was stupidly not checking the exact line number of the warning. I can confirm the code you pasted works without any deprecated warnings.
Dave