tags:

views:

58

answers:

2

It should be something like this:

function callcenter($func,$value,$position)
{

}

Where $func is the function to be called,

$value is the parameter for $func,

and $position stands for index of $value,

for example,

callcenter('func',1,2) should actually call func(null,1)

callcenter('func',1,3) should actually call func(null,null,1).

Say,leaving other positions as null.

A: 

In php it is posible to call a function whose name is stored in a varable, so

function callcenter($func,$value,$position)
{
 switch ($position)
  {
   case 1: $func($value); break;
   case 2: $func(null, $value); break;
   case 3: $func(null, null, $value); break;
  }
}

There might be a better way to handle the variable number of parameters, but that should get the basic idea across.

Tom
What if $position is 100000?The switch will be extremely long,lol~
Shore
Yep, see my comment to Matthew
Tom
+2  A: 

You're after call_user_func_array and array_fill

<?php
function callcenter($func, $value, $position)
{
    $args = array_fill(0, $position-1, null);
    $args[] = $value;

    call_user_func_array($func, $args);
}

function example()
{
    $args = func_get_args();

    var_dump($args);
}

callcenter('example',1,2);

callcenter('example',1,3);
?>
Matthew
As I thought, there is a better way than my switch statement. I'll file this one for future reference. +1
Tom