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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错