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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算