简体   繁体   中英

php use an array to pass in arguments to a dynamic function

I've searched the web high and low for any answer on this, but have found nothing.

Here is what I'm trying to accomplish:

I have a routing function in an mvc framework, and currently this is how arguments are being passed into the function.

$this->url_controller->{$this->url_action}($this->url_parameter_1, $this->url_parameter_2, $this->url_parameter_3);

It's good, but limits it to only 3 parameters, or how ever many I hard code in. I would like to make this function more dynamic. The actual function that it's calling is expecting 4 arguments. What I would like to do is this:

$params = array();
foreach( $this->url_parameters as $key => $value ):
    if($key == 0) continue;
    if($key == 1) continue;
    $params[] = $this->url_parameters[$key];
endforeach;
$this->url_controller->{$this->url_action}($params);

Now I understand why this doesn't work. I've tried using call_user_func_array() but because of the dynamic naming it wouldn't work correctly. I'm also trying to avoid using func_get_args() on the receiving end to keep things simple.

Any help is appreciated. :)

在接收端使用func_get_args()处理所有参数,然后循环遍历,因为它是一个数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM