douweng7233 2009-12-14 17:38
浏览 102
已采纳

通过引用将参数可移植地传递给PHP ReflectionMethod :: invokeArgs

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.

  • 写回答

1条回答 默认 最新

  • douying0108 2009-12-14 19:31
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的