func_get_args

(PHP 4, PHP 5)

func_get_args — Возвращает массив аргументов функции

Описание

array func_get_args ( void )

Возвращает массив, в котором каждый элемент является соответствующим членом списка аргументов пользовательской функции. func_get_args() генерирует предупреждение при вызове вне определения функции.

<?php
function foo() 
{
    
$numargs func_num_args();
    echo 
"Number of arguments: $numargs<br />\n";
    if (
$numargs >= 2) {
        echo 
"Second argument is: " func_get_arg(1) . "<br />\n";
    }
    
$arg_list func_get_args();
    for (
$i 0$i $numargs$i++) {
        echo 
"Argument $i is: " $arg_list[$i] . "<br />\n";
    }


foo(123);
?>

func_get_args() может быть использована совместно с func_num_args() и func_get_arg() для создания функций с переменным количеством аргументов.

Коментарии

Автор:
You can pass a variable number of arguments to a function whilst keeping references intact by using an array. The disadvantage of course, is that the called function needs to be aware that it's arguments are in an array.

<?php
// Prints "hello mutated world"
function mutator($args=null) {
$n=count($args);
while(
$i<$n$args[$i++] = "mutated";
}
$a "hello";
$b "strange";
$c "world";
mutator(array($a, &$b$c));
echo 
"$a $b $c";
?>
2001-06-04 10:44:31
http://php5.kiev.ua/manual/ru/function.func-get-args.html
<?php
// How to simulate named parameters in PHP.
// By Dave Benjamin <dave@ovumdesign.com>

// Turns the array returned by func_get_args() into an array of name/value
// pairs that can be processed by extract().
function varargs($args) {
   
$count count($args);
    for (
$i 0$i $count$i += 2) {
       
$result[$args[$i]] = $args[$i 1];
    }
   
    return 
$result;
}

// Example
function test(&$ref1, &$ref2) {
   
// Default arguments go here.
   
$foo "oof";
   
   
// Do some magic.
   
extract(varargs(func_get_args()));

    echo 
nl2br("\n\$var1 = $var1");
    echo 
nl2br("\n\$var2 = $var2");
    echo 
nl2br("\n\$foo = $foo\n\n");
   
   
// Modify some variables that were passed by reference.
    // Note that func_get_args() doesn't pass references, so they
    // need to be explicitly declared in the function definition.
   
$ref1 42;
   
$ref2 84;
}

$a 5;
$b 6;

echo 
nl2br("Before calling test(): \$a = $a\n");
echo 
nl2br("Before calling test(): \$b = $b\n");

// Try removing the 'foo, "bar"' from the following line.
test($a$bvar1"abc"var2"def"foo"bar");

echo 
nl2br("After calling test(): \$a = $a\n");
echo 
nl2br("After calling test(): \$b = $b\n");
?>
2001-09-18 02:29:10
http://php5.kiev.ua/manual/ru/function.func-get-args.html
Автор:
Simple function to calculate average value using dynamic arguments:
<?php
function average(){
    return 
array_sum(func_get_args())/func_num_args();
}
print 
average(10152025); // 17.5
?>
2004-11-04 09:24:33
http://php5.kiev.ua/manual/ru/function.func-get-args.html
"Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable."

This means that the following code generates an error:

<?php

function foo($list)
{
  echo 
implode(', '$list);
}

function 
foo2()
{
 
foo(func_get_args());
}

foo2(123);

?>

However, you can easily get around this by doing the following:

<?php

function foo($list)
{
  echo 
implode(', '$list);
}

function 
foo2()
{
 
foo($args func_get_args());
}

foo2(123);

?>

This captures the context from foo2(), making this legal.  You get the expected output:

"1, 2, 3"
2007-05-07 17:50:58
http://php5.kiev.ua/manual/ru/function.func-get-args.html
How to create a polymorphic/"overloaded" function

<?php
function select()
{
   
$t '';
   
$args func_get_args();
    foreach (
$args as &$a) {
       
$t .= gettype($a) . '|';
       
$a mysql_real_escape_string($a);
    }
    if (
$t != '') {
       
$t substr($t0, - 1);
    }
   
$sql '';
    switch (
$t) {
        case 
'integer':
           
// search by ID
           
$sql "id = {$args[0]}";
            break;
        case 
'string':
           
// search by name
           
$sql "name LIKE '%{$args[0]}%'";
            break;
        case 
'string|integer':
           
// search by name AND status
           
$sql "name LIKE '%{$args[0]}%' AND status = {$args[1]}";
            break;
        case 
'string|integer|integer':
           
// search by name with limit
           
$sql "name LIKE '%{$args[0]}%' LIMIT {$args[1]},{$args[2]}";
            break;
        default:
           
// :P
           
$sql '1 = 2';
    }
    return 
mysql_query('SELECT * FROM table WHERE ' $sql);
}
$res select(29); // by ID
$res select('Anderson'); // by name
$res select('Anderson'1); // by name and status
$res select('Anderson'05); // by name with limit
?>
2008-08-27 02:25:58
http://php5.kiev.ua/manual/ru/function.func-get-args.html
<?php
/*
This example demonstrate how to use unknown variable arguments by reference.
func_get_args() don't return arguments by reference, but 
debug_backtrace() "args" is by reference.
In PHP 5 this have no particular sense, because calling with arguments by reference
is depreciated and produce warning.
*/

class foo {

    var 
$bar "default bar";
   
    function 
foo(/*variable arguments*/) {
// func_get_args returns copy of arguments
//        $args = func_get_args();
// debug_backtrace returns arguments by reference           
       
$stack debug_backtrace();
       
$args = array();
        if (isset(
$stack[0]["args"]))
            for(
$i=0$i count($stack[0]["args"]); $i++)
               
$args[$i] = & $stack[0]["args"][$i];
       
call_user_func_array(array(&$this'bar'), $args);
    }
   

    function 
bar($bar NULL) {
        if (isset(
$bar))
           
$this->bar = & $bar
    }
}

$global_bar "bar global";
$foo = & new foo();
echo 
"foo->bar:    ".$foo->bar."</br>\n";
$foo->bar "new bar";
echo 
"global_bar:  ".$global_bar."</br>\n";
/*
Result:
foo->bar:    default bar</br>
global_bar:  bar global</br>
*/

$foo = & new foo(&$global_bar);
echo 
"foo->bar:    ".$foo->bar."</br>\n";
$foo->bar "new bar";
echo 
"global_bar:  ".$global_bar."</br>\n";
/*
Result:
foo->bar:    bar global</br>
global_bar:  new bar</br>
*/

?>
2009-04-06 06:48:45
http://php5.kiev.ua/manual/ru/function.func-get-args.html
Автор:
please note that optional parameters are not seen/passed by func_get_args(), as well as func_get_arg().

ex:

<?php
function testfunc($optional 'this argument is optional..') {
   
$args func_get_args();
   
var_dump($args);
    echo 
$optional;
}
?>

test case #1:
testfunc('argument no longer optional..');

result for #1:
array(1) {
    [0]=>  string(20) "argument no longer optional.."
}
argument no longer optional..

test case #2:
testfunc('argument no longer optional..','this is an extra argument');

result for #2:
array(2) {
    [0]=>  string(29) "argument no longer optional.."
    [1]=>  string(25) "this is an extra argument"
}
argument no longer optional..

test case #3: -- RESULTS IN AN EMPTY ARRAY
testfunc();

result for #3:
array(0) {
}
this argument is optional..
2011-12-30 21:54:08
http://php5.kiev.ua/manual/ru/function.func-get-args.html
it seems that this function only returns a copy and loses it's byref information, use this dirty non-efficient workaround instead:

at the moment of writing it currently returns all of them as references, instead of only the ones who are passed that way...

<?php
function func_get_args_byref() {
       
$trace debug_backtrace();
        return 
$trace[1]['args'];
}
?>
2012-09-13 14:58:56
http://php5.kiev.ua/manual/ru/function.func-get-args.html
Merge func_get_args() with function defaults
<?php
class utils {
 
/**
   * @param mixed[] $args
   * @param ReflectionMethod $reflectionMethod
   *
   * @return array
   */
 
public static function mergeArgsWithDefaults$args\ReflectionMethod $reflectionMethod ) {
    foreach ( 
array_slice$reflectionMethod->getParameters(), count$args ) ) as $param ) {
     
/**
       * @var ReflectionParameter $param
       */
     
$args[] = $param->getDefaultValue();
    }
    return 
$args;
  }
}

class 
sampleParent {
  const 
USER_FILE_TYPE_FILE 'FILE';
  public function 
select$idUserFile null$idUserFileType self::USER_FILE_TYPE_FILE ) {
    echo 
'[$idUserFile=>' $idUserFile ', $idUserFileType=>' $idUserFileType']<br/>' PHP_EOL;
  }
}

class 
sample extends sampleParent {
  const 
USER_FILE_TYPE_IMG 'IMG';
  public function 
select$idUserFile null$idUserFileType self::USER_FILE_TYPE_IMG ) {
    return 
call_user_func_array'parent::select'\utils::mergeArgsWithDefaultsfunc_get_args(), new ReflectionMethod__CLASS____FUNCTION__ ) ) );
  }
}

$sample1 = new sampleParent();
$sample1->select();//Prints "" / self::USER_FILE_TYPE_FILE
$sample1->select(1);//Prints 1 / self::USER_FILE_TYPE_FILE
$sample1->select(2'test 1');//Prints 2 / "test 1"
echo '<br/>' PHP_EOL;
$sample2 = new sample();
$sample2->select();//Prints "" / self::USER_FILE_TYPE_IMG
$sample2->select(3);//Prints 3 / self::USER_FILE_TYPE_IMG
$sample2->select(4'test 2');//Prints 4 / "test 2"
?>
2016-03-17 20:18:21
http://php5.kiev.ua/manual/ru/function.func-get-args.html
If you want to get the arguments by reference, instead of func_get_args() you can simply use

<?php
function args_byref(&...$args) {
   
// Modify the $args array here
}
?>

Credits should go to Markus Malkusch for pointing this out on Stackoverflow.
https://stackoverflow.com/a/29181826/1426064
2019-11-20 18:17:52
http://php5.kiev.ua/manual/ru/function.func-get-args.html
The size of the array resulting from func_get_args(), for instance using count(), does not take into account parameters that have been assigned default values in the function definition.

Example:

function foo($bar=true) {
     echo count(func_get_args());
}

foo(); 
// echoes 0

foo("bar"); 
// echoes 1

A useful condition to test for when a function needs to return default behavior (whatever that might be) when no value is present and the value of $bar could be true, false, null, etc.
2023-03-28 18:32:43
http://php5.kiev.ua/manual/ru/function.func-get-args.html
I wanted an associative list of arguments, in case some else does too, I'm leaving it here.

I hope PHP gets native support for this, because a core implementation would be faster than this userland backtrace+reflection implementation:
<?php

/**
 * Get function arguments as associative array
 * (same as func_get_args() but with keys)
 * 
 * @param bool $populateMissingArgumentsWithDefaults whether to populate the array with default values for missing arguments
 * 
 * @return array
 */
function func_get_args_associative(bool $populateMissingArgumentsWithDefaults false): array
{
   
$trace debug_backtrace(02)[1];
   
$reflection null;
    if (isset(
$trace['class'])) {
       
$reflection = new \ReflectionMethod($trace['class'], $trace['function']);
    } else {
       
$reflection = new \ReflectionFunction($trace['function']);
    }
   
$ret = [];
    foreach (
$reflection->getParameters() as $param) {
        if (
array_key_exists($param->getPosition(), $trace['args'])) {
           
$ret[$param->name] = $trace['args'][$param->getPosition()];
        } elseif (
$populateMissingArgumentsWithDefaults) {
           
// because of the "required arguments declared after an optional argument are implicitly required" rule:
           
assert($param->isDefaultValueAvailable(), "i think all params are either in trace[args] or have default values");
           
$ret[$param->name] = $param->getDefaultValue();
        }
    }
    return 
$ret;
}
?>
2023-10-20 12:34:39
http://php5.kiev.ua/manual/ru/function.func-get-args.html

    Поддержать сайт на родительском проекте КГБ