|
|
 |
call_user_func_array (PHP 4 >= 4.0.4, PHP 5) call_user_func_array --
Llamar una función de usuario dada con una matriz de
parámetros
Descripciónmixed call_user_func_array ( callback funcion, array matriz_param )
Llamar una función definida por el usuario dada por
funcion, con los parámetros en
matriz_param. Por ejemplo:
Ejemplo 1. Ejemplo de
call_user_func_array() |
<?php
function depurar($var, $val)
{
echo "***DEPURANDO\nVARIABLE: $var\nVALOR:";
if (is_array($val) || is_object($val) || is_resource($val)) {
print_r($val);
} else {
echo "\n$val\n";
}
echo "***\n";
}
$c = mysql_connect();
$host = $_SERVER["SERVER_NAME"];
call_user_func_array('depurar', array("host", $host));
call_user_func_array('depurar', array("c", $c));
call_user_func_array('depurar', array("_POST", $_POST));
?>
|
|
Vea también call_user_func(), y
information about the callback type
add a note
User Contributed Notes
call_user_func_array
26-Jul-2006 12:22
Re: amer at o2 dot pl 's comment:
"If you need to call object method from within the very same object (NOT CLASS!):
call_user_func_array(array(&$this, 'Foo'),args);"
This particular case does not work--giving the following error:
call_user_func_array(): First argumented is expected to be a valid callback, 'Array' was given
The user functions can only be successfully called (as the documentation for call_user_func alludes to), with static calls to class functions.
15-May-2006 02:43
For those wishing to implement call-by-name functionality in PHP, such as implemented e.g. in DB apis, here's a quick-n-dirty version for PHP 5 and up
<?php
function call_user_func_named($function, $params)
{
if (!function_exists($function))
{
trigger_error('call to unexisting function '.$function, E_USER_ERROR);
return NULL;
}
$reflect = new ReflectionFunction($function);
$real_params = array();
foreach ($reflect->getParameters() as $i => $param)
{
$pname = $param->getName();
if ($param->isPassedByReference())
{
}
if (array_key_exists($pname, $params))
{
$real_params[] = $params[$pname];
}
else if ($param->isDefaultValueAvailable()) {
$real_params[] = $param->getDefaultValue();
}
else
{
trigger_error(sprintf('call to %s missing parameter nr. %d', $function, $i+1), E_USER_ERROR);
return NULL;
}
}
return call_user_func_array($function, $real_params);
}
?>
crocodile2u at yandex dot ru
20-Dec-2005 05:06
Here is another version of createObjArray() function written here earlier by taylor.
Believing that using 'eval()' is at least "dirty", I came to the following solution (with a help of panchous - at phpclub dot ru forums ). This solution utilizes the new Reflection API.
<?php
function & createObjArray($type, $args = array()) {
$reflection = new ReflectionClass($type);
$output = call_user_func_array(array(&$reflection, 'newInstance'), $args);
return $output;
}
?>
Egor
15-Nov-2005 09:35
Note that, despite the name, this does work on builtin functions (and object methods with the array(&$obj, $method) syntax), not just user-defined functions and methods.
rrant (at) gmail (dot) com
10-Nov-2005 03:50
Just an extra for the post of amer at o2 dot pl:
If you need to call the PARENT method:
call_user_func_array(array('parent', 'method'), $args);
With that, if you need to call a constructor and/or add some extra code to the instantiation process:
<?php
function __construct() {
$args = func_get_args();
call_user_func_array(array('parent', '__construct'), $args);
}
?>
Note that your constructor pass all the arguments to the parent constructor and it doesn't matter how many arguments you pass.
This is pretty useful for constructors with a variable number of arguments.
taylor
02-Jun-2005 05:51
I came up with a better solution to the problem that I solve below with createObjArray that maintains parameter type:
<?php
function createObjArray($type,$args=array()) {
$paramstr = '';
for ($i = 0; $i < count($args); $i++) {
$paramstr .= '$args['.$i.'],';
}
$paramstr = rtrim($paramstr,',');
return eval("return new $type($paramstr);");
}
?>
Would be good to add error checking, but it works.
taylor
07-May-2005 03:04
<?php
function createObjArray($type, $args=array()) {
if ( !class_exists($type) ) {
return NULL;
}
$func = create_function('$str', 'return str_replace("\'","\\\'",$str);');
$sargs = "'" . join( "','", array_map($func,$args) ). "'";
$seval = "return new $type($sargs);";
return eval($seval);
}
?>
amer at o2 dot pl
20-Jan-2005 02:44
PLS notice that "patripaq at hotmail dot com" 's code will be valid if B EXTENDS A...
<?php
class B extends A{
...
}
?>
there>>"What I wanted to do is create an object that can manage any number and any kind of parameters."
BUT IT IS NOT A POINT AT ALL
If you need to call just function with parameters:
call_user_func_array('Foo',$args);
If you need to call CLASS method (NOT object):
call_user_func_array(array('class', 'Foo'),$args);
If you need to call OBJECT method:
call_user_func_array(array(&$Object, 'Foo'),$args);
If you need to call method of object of object:
call_user_func_array(array(&$Object->Object, 'Foo'),$args);
If you need to call object method from within the very same object (NOT CLASS!):
call_user_func_array(array(&$this, 'Foo'),args);
The call_user_func_array ITSELF can manage any number and any kind of parameters. It can handle ANY FUNCTION too as it is defined and that maybe partipaq wanted to manage.
What You actually need is object composition not inheritance. Make an instance from arguments.
<?php
...
class B{
function __construct() {
$args = func_get_args(); $this->OBJ = new A($args);
call_user_func_array(array(&$this->OBJ, 'A'), $args );
}
}
?>
Then there can be any number and any type of created object B parameters
james at gogo dot co dot nz
21-Nov-2004 05:19
Be aware the call_user_func_array always returns by value, as demonstrated here...
<?php
function &foo(&$a)
{
return $a;
}
$b = 2;
$c =& call_user_func_array('foo', array(&$b));
$c++;
echo $b . ' ' . $c;
?>
outputs "2 3", rather than the expected "3 3".
Here is a function you can use in place of call_user_func_array which returns a reference to the result of the function call.
<?php
function &ref_call_user_func_array($callable, $args)
{
if(is_scalar($callable))
{
$call = $callable;
}
else
{
if(is_object($callable[0]))
{
$call = "\$callable[0]->{$callable[1]}";
}
else
{
$call = "{$callable[0]}::{$callable[1]}";
}
}
$argumentString = array();
$argumentKeys = array_keys($args);
foreach($argumentKeys as $argK)
{
$argumentString[] = "\$args[$argumentKeys[$argK]]";
}
$argumentString = implode($argumentString, ', ');
eval("\$result =& {$call}({$argumentString});");
return $result;
}
?>
php at pjt33 dot f2g dot net
25-Oct-2004 05:31
Note that, although it doesn't say so here or in the linked page with information about the callback type, the changelog ( http://www.php.net/ChangeLog-4.php#4.0.5 ) states that call_user_func_array in PHP 4.0.4 won't take an array as the first argument. This was added in 4.0.5.
patripaq at hotmail dot com
06-Aug-2004 03:49
I just started using PHP 5.0 and, so far, I'm loving it ! However, I had a problem the other day and thought it would be a good idea to inform other programmers about the solution I found to get around it. It concerns the new __constructor() function and the call_user_func_array() function. What I wanted to do is create an object that can manage any number and any kind of parameters. Here's the problematic code:
<?php
class A {
function __construct() {
$args = func_get_args(); }
function A() {
$args = func_get_args(); call_user_func_array( array(&$this, '__construct'), $args ); }
}
class B {
function __construct() {
$args = func_get_args(); call_user_func_array( array(&$this, 'A'), $args ); }
}
$obj = new B( 'param1', 'param2' );
?>
I suppose you can guess where the problem is located... In the A::A() function, the call to __construct() using call_user_func_array() is redirected to B::__construct() instead of A::__construct(). The only way I found to specify which constructor function I wanted to call was to stop using A::__construct() and use the old fashion constructor instead. If anyone can find a better way, feel free to add comments. Here's my solution. Hope it helps anyone.
<?php
class A {
function A() {
$args = func_get_args(); }
}
class B {
function __construct() {
$args = func_get_args(); call_user_func_array( array(&$this, 'A'), $args ); }
}
$obj = new B( 'param1', 'param2' );
?>
hong dot nguyen at k-edge dot com
02-Apr-2004 09:37
call_user_func_array can pass parameters as reference:
<?php
call_user_func_array(array(&$obj,$method),array(&$arg1,$arg2,$arg3))
?>
Use it as work-around for "Call-time pass-by-reference has been deprecated".
mightye (at) mightye (dot) org
12-Mar-2004 09:19
From what I can tell, call_user_func_array *always* passes by value, even if the function asks for a reference (perhaps it duplicates the args before passing them?).
adamh at densi dot com
31-Jul-2003 11:58
call_user_func_array() is nifty for calling PHP functions which use variable argument length. For example:
<?php
$array = array(
array("foo", "bar"),
array("bat", "rat"),
);
$values = call_user_func_array("array_merge", $array);
var_dump($values);
?>
/* output:
array(4) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "bat"
[3]=>
string(3) "rat"
}
*/
The neat feature is that $array could have any number of arrays inside it.
gord at fig dot org
06-May-2003 04:30
If you need to call object and class methods in PHP < 4.0.4, the following code ought to do the trick:
<?php
if (!function_exists('call_user_func_array')) {
function call_user_func_array($func, $args)
{
$argString = '';
$comma = '';
for ($i = 0; $i < count($args); $i ++) {
$argString .= $comma . "\$args[$i]";
$comma = ', ';
}
if (is_array($func)) {
$obj =& $func[0];
$meth = $func[1];
if (is_string($func[0])) {
eval("\$retval = $obj::\$meth($argString);");
} else {
eval("\$retval = \$obj->\$meth($argString);");
}
} else {
eval("\$retval = \$func($argString);");
}
return $retval;
}
}
?>
nutbar at innocent dot com
12-Mar-2003 12:29
This function, combined with some others, can allow you to make some really easy & generic function macros (since real macros don't exist in PHP yet).
<?php
function my_printf() {
$args = func_get_args();
return call_user_func_array('printf', $args);
}
?>
This allows you to use my_printf() just as if it were printf() itself. Very useful since you don't have to worry about how many arguments any functions take.
NOSPAM dot dont dot remove at thekid dot de
22-Jun-2002 04:19
Unified constructors in PHP4:
<?php
class Object {
function Object() {
$args= func_get_args();
call_user_func_array(array(&$this, '__construct'), $args);
}
function __construct($args= NULL) {
var_dump($args);
}
}
class Exception extends Object {
var
$message;
function __construct($message) {
$this->message= $message;
parent::__construct();
}
}
class IOException extends Exception {
}
var_dump(
error_reporting(),
zend_version(),
phpversion()
);
$c= new IOException('file not found');
echo '===> Result: '; var_dump($c);
?>
| |
| | Citas célebres | A luke Skywalker en El Imperio Contraataca (1980): O lo haces o no lo haces. Nada de "lo intentaré". Maestro Yedi Yoda | | Citas en tu mail | | ©Contenidos Gratis |
| Inicio | Acción | Estrategia | Palabras | Puzzles | Solitarios | Foro Trucos |  | Cake Mania Jugadores: 6835 Categoría del juego: Acción Objetivo del juego: Ayuda a Jill a recuperar la pastelería de su abuela llevando su propia pastelería; consigue clientes y gana dinero. |
|  | Rainbow Web Jugadores: 2199 Categoría del juego: Puzzles Objetivo del juego: Rompe un pegajoso hechizo y salva un reino de fantasía en Rainbow Web. Tendrás toneladas de diversión mientras juegas a este mágico desafío para la mente. |
|  | Mahjongg Fortuna Jugadores: 12462 Categoría del juego: Solitarios Objetivo del juego: Velocidad y habilidad mental son las armas más importantes en esta versión de un antiguo juego asiático. Despeja el tablero lo antes posible haciendo clic en las fichas iguales y gánate la fama eterna de la puntuación más alta. |
|  | Chainz 2 Jugadores: 6955 Categoría del juego: Puzzles Objetivo del juego: Entra en el mundo de las combinaciones con Chainz 2: Relinked, emocionante secuela del exitazo del año pasado, Chainz. Gira eslabones y crea combinaciones de 3 ó más. |
|  | Delicious Jugadores: 4405 Categoría del juego: Acción Objetivo del juego: ¿Eres un as de la multitarea? ¿Quieres que tus clientes estén contentos? ¡Pues Delicious es tu juego! Sacia el apetito de los clientes y tenlos contentos; ¡no te arriesgues! |
|  | Bookworm Jugadores: 4568 Categoría del juego: Palabras Objetivo del juego: Junta las letras para formar palabras. ¡Las palabras más largas valen más puntos! |
|  | Zuma Jugadores: 4976 Categoría del juego: Acción Objetivo del juego: Controla el ídolo de la rana de piedra de los antiguos Zuma en este intrigante enigma de acción. ¡Dispara bolas para formar conjuntos de tres, pero si dejas que lleguen a la calavera dorada morirás! |
|  | Jewel of Atlantis Jugadores: 3798 Categoría del juego: Puzzles Objetivo del juego: Descubre la ciudad hundida de la Atlántida y busca valiosos tesoros. Viaja más allá de las profundidades del mar y vive trepidantes aventuras en Jewel of Atlantis. |
|  | Jewel Quest Jugadores: 3727 Categoría del juego: Puzzles Objetivo del juego: Convierte la arena de la antigua selva en oro tan rápido como puedas juntando grupos de 3 elementos. ¡Los grupos más grandes valen más puntos! |
|  | Bejeweled 2 Jugadores: 3659 Categoría del juego: Puzzles Objetivo del juego: Con cuatro modos de juego únicos y fascinantes, nuevas piezas de juego explosivas e imponentes fondos planetarios, Bejeweled 2 es mucho más adictivo que nunca. |
|
|