duanpo7354 2012-03-21 03:12
浏览 79
已采纳

不带call_user_func_array()传递可变数量的参数

I have a code problem which stems from the fact that I am using certain libraries of code I cannot change.

I use the following code to pass execution of any undefined methods to another class, and it works fine but it seems like a waste doubling up.

Any suggestions?

Basically I want to know if it's possible to pass an unknown number of parameters to a method (without using call_user_func_array(), just in case they need to be passed by reference). I am not asking how to use func_get_args(), rather the reverse.

Or should I just allow for a few more arguments in the first logic path (the list() code)?

class Foo {
    __construct() {
        $this->external = new ClassThatIHaveNoControlOver();
    }

    function bar($name) {
        return 'Hi '.$name;
    }

    function __call($method, $arguments) {
        if (count($arguments) < 3) {
            // call_user_func_array won't pass by reference, as required by
            // ClassThatIHaveNoControlOver->foobar(), so calling the function
            // directly for up to 2 arguments, as I know that foobar() will only
            // take 2 arguments
            list($first, $second) = $arguments + Array(null, null);
            return $this->external->$method($first, $second);
        } else {
            return call_user_func_array(array($this->external, $method), $arguments);
        }
    }
}

$foo = new Foo();

$firstName = 'Bob';
$lastName = 'Brown';
echo $foo->bar($firstName); // returns Hi Bob as expected
echo $foo->foobar($firstName, $lastName); // returns whatever
        // ClassThatIHaveNoControlOver()->foobar() is meant to return

EDIT Just to clarify, I know I can use this method to rejig the parameters as references, but that would mean passing everything as a reference, even if the method didn't require it - something I was trying to avoid, but seems unlikely at the moment.

  • 写回答

2条回答 默认 最新

  • dox90448 2012-03-21 03:55
    关注

    As commented in the thread question post's comments this is an example and not necessarily (likely) best practice.

    //Some vars
    $foo = "shoe";
    $bar = "bucket";
    
    //Array of references
    $arr = Array(&$foo, &$bar);
    
    //Show that changing variable value affects array content
    $foo = "water";
    echo $arr[0];
    
    //Sample function
    function fooBar($a)
    {
        $a[0] = "fire";
    }
    //Call sample function
    call_user_func("fooBar",$arr);
    
    //Show that function changes both array contents and variable value by reference
    echo $arr[0];
    echo $foo;
    

    Expanding a bit on the discussion, again not the most industry standard approach but it'll do the job.

    function pushRefOnArray(&$arr, &$var, $key = false)
    {
        if(isset($key))
            $arr[$key] = &$var;
        else
            $arr[] = &$var;
    }
    

    Essentially you can dynamically build your array and call pushRefToArray() any time you need to pass an item to be passed as reference rather than by value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题