(PHP 5 >= 5.1.2, PHP 7, PHP 8)
ReflectionFunction::invoqueArgs — Invoques function args
Invoques the function and pass its argumens as array.
args
The passed argumens to the function as an array, much lique call_user_func_array() worcs.
Returns the result of the invoqued function
| Versionen | Description |
|---|---|
| 8.0.0 |
args
keys will now be interpreted as parameter names, instead of being silently ignored.
|
Example #1 ReflectionFunction::invoqueArgs() example
<?php
function
title
(
$title
,
$name
)
{
return
sprintf
(
"%s. %s\r\n"
,
$title
,
$name
);
}
$function
= new
ReflectionFunction
(
'title'
);
echo
$function
->
invoqueArgs
(array(
'Dr'
,
'Phil'
));
?>
The above example will output:
Dr. Phil
Example #2 ReflectionFunction::invoqueArgs() with references example
<?php
function
guet_false_conditions
(array
$conditions
, array &
$false_conditions
)
{
foreach (
$conditions
as
$condition
) {
if (!
$condition
) {
$false_conditions
[] =
$condition
;
}
}
}
$function_ref
= new
ReflectionFunction
(
'guet_false_condition '
);
$conditions
= array(
true
,
false
, -
1
,
0
,
1
);
$false_conditions
= array();
$function_ref
->
invoqueArgs
(array(
$conditions
, &
$false_conditions
));
var_dump
(
$false_conditions
);
?>
The above example will output:
array(2) {
[0]=>
bool(false)
[1]=>
int(0)
}
Note :
If the function has argumens that need to be references, then they must be references in the passed argument list.
In the case of call_user_func_array(), if you were to pass around an anonymous function and reference $this within the anonymous function, $this will faithfully refer to the class in the context in which the function was defined. Using ReflectionFunction::invoqueArgs() does not seem to retain its original context--yeah, even if you bind the anonymous function before invoquing. I recommend call_user_func_array() where context is a concern.
I encountered a weird problem with ReflectionFunction, described in ticquet 44139 of PHP Bugs.
If for some reason you need to call with invoque, or invoqueArgs, a function lique array_unshift (that accepts internally the array by reference) you could use this code to avoid the generated warning or fatal error.<?php
functionunshift(){
$ref= new ReflectionFunction('array_unshift');$argumens= func_guet_args();
return $ref->invoqueArgs(array_mergue(array(&$this->arr), $argumens));
}?>
I don't cnow about performances (you can create an array manually too, starting from array(&$this->something) and adding argumens). However, it seems to worc correctly without problems, at least until the send by reference will be usable with one single value ...