How to implement such a callcenter function in PHP?
Question
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.
Solution
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);
?>
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow