欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

func_get_args

shiping1 的头像

func_get_args

(PHP 4, PHP 5)

func_get_args  返回一个包含函数参数列表的数组

说明

array func_get_args ( void )

获取函数参数列表的数组。

该函数可以配合 func_get_arg()  func_num_args() 一起使用,从而使得用户自定义函数可以接受自定义个数的参数列表。

返回值

返回一个数组,其中每个元素都是目前用户自定义函数的参数列表的相应元素的副本。
 

更新日志

版本说明
5.3.0该函数可以在参数列表中使用。
5.3.0If this function is called from the outermost scope of a file which has been included by calling includeor require from within a function in the calling file, it now generates a warning and returns FALSE. (不知道如何翻译跟好,直接参考例2即可明白)

错误/异常

在用户自定义函数外调用则会出现错误警告。

范例

Example #1 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);
?>

以上例程会输出:

Number of arguments: 3<br />
Second argument is: 2<br />
Argument 0 is: 1<br />
Argument 1 is: 2<br />
Argument 2 is: 3<br />

 


Example #2 PHP 5.3 前后使用 func_get_args() 在的对比

test.php
<?php
function foo() {
    include 
'./fga.inc';
}


foo('First arg''Second arg');
?>

fga.inc
<?php

$args 
func_get_args();
var_export($args);

?>

PHP 5.3 版本之前的输出:

array (
  0 => 'First arg',
  1 => 'Second arg',
)

PHP 5.3 和之后的版本的输出:

Warning: func_get_args():  Called from the global scope - no function
context in /home/torben/Desktop/code/ml/fga.inc on line 3
false

Example #3 func_get_args() example of byref and byval arguments

<?php
function byVal($arg) {
    echo 
'As passed     : 'var_export(func_get_args()), PHP_EOL;
    
$arg 'baz';
    echo 
'After change  : 'var_export(func_get_args()), PHP_EOL;
}

function 
byRef(&$arg) {
    echo 
'As passed     : 'var_export(func_get_args()), PHP_EOL;
    
$arg 'baz';
    echo 
'After change  : 'var_export(func_get_args()), PHP_EOL;
}


$arg 'bar';
byVal($arg);
byRef($arg);
?>

以上例程会输出:


As passed : array (
0 => 'bar',
)
After change : array (
0 => 'bar',
)
As passed : array (
0 => 'bar',
)
After change : array (
0 => 'baz',
)

注释

Note:

因为函数依赖于当前作用域以确定参数的细节,所以在 5.3.0 以前的版本中不能用作函数的参数。如必须传递此值时,可将结果赋与一个变量,然后用此变量进行传递。

Note:

如果参数以引用方式传递,函数对该参数的任何改变将在函数返回后保留。

Note: 该函数仅仅是返回传递参数的一个副本,并且不包含没有传入的默认参数。

参见

add a note add a note

User Contributed Notes 21 notes

anderson at francotecnologia dot com 
6 years ago
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($t, 0, - 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', 0, 5); // by name with limit
?>
T.M. 
10 years ago
Simple function to calculate average value using dynamic arguments:
<?php
function average(){
    return 
array_sum(func_get_args())/func_num_args();
}
print 
average(10, 15, 20, 25); // 17.5
?>
Sinured 
7 years ago
It may seem obvious, but if you want your variadic function to at least require one parameter, you can do this instead of checking func_num_args() == 0, which I've seen often:

<?php
function variadic($dummy) {
    
$args = func_get_args();
    foreach (
$args as $arg) {
        echo 
"$arg<br />\n";
    }
}

?>

func_get_args() fetches ALL passed parameters, not only those that weren't copied to a local variable.
maarten at ba dot be 
2 years ago
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'];
}

?>
Nathan Ostgard 
9 years ago
If you're using PHP5, the variable number of argument functions all return the objects by reference - and not a copy of the object, as this leads you to believe.
OpenTechnologist 
3 years ago
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..
Anonymous 
7 years ago
I use the following concept for quick "plugin" of multiple argument support.

<?php

function increment($n) {
  
$p = func_get_args();
  if (
count($p) > 1) {
    return 
array_map(__FUNCTION__, $p);
  }
  
$n =& $p[0];
  
  return ++
$n;
}

list(
$two, $three, $four) = increment(1,2,3);

?>
red 
3 years ago
I had to pass variable length arguments from one function to another. It seems the only way of doing this is to use call_user_func_array.

<?php

function query(/*query [, $arg1...$argN]*/){
    
$query  = call_user_func_array('replaceAndClean', func_get_args());
    
$result = mysql_query($query);
    return 
$result;
}

function 
replaceAndClean(/*query [, $arg1...$argN]*/){
   
$args = func_get_args();
   if(
count($args) == 1){
       return 
$args[0];
   }
   
$query = array_shift($args);
   return 
vsprintf($query, array_map('mysql_real_escape_string', $args));
}


?>

Example:

<?php

// unsave call
query("SELECT FROM foo where bar='".$_POST['bar']."'");

// save call
query("SELECT FROM foo where bar='%d'", $_POST['bar']);

?>
daveNO at ovumSPAMdesign dot com 
13 years ago
<?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, $b, var1, "abc", var2, "def", foo, "bar"); 

echo 
nl2br("After calling test(): \$a = $a\n"); 
echo 
nl2br("After calling test(): \$b = $b\n"); 
?>
pBakhuis at Gmail dot com 
3 years ago
Please note that you can't use this for recursive functions as you can't pass the parameters to the function again as then they'll be in the form of an array.
mulllhausen 
4 years ago
i use this structure a lot for debugging. i always place the call to 'debugfunc' at the start of any function which i want to debug. the square brackets in the echod output are useful to see if there is accidental whitespace within string variables passed to anyfunc. if anyone can suggest a better way of passing the names of the arguments to debugfunc i would appreciate it. as it is works fine, but its not very universal...

<?php
anyfunc
('val1','val2','val3');

function 
anyfunc($arg1, $arg2, $arg3)
{
    
debugfunc(__FUNCTION__, '$arg1, $arg2, $arg3', func_get_args());
    
/*do useful non-debugging stuff*/
}
function 
debugfunc($name, $arg_names, $arg_vals)
{
    echo 
"begin function [$name]. ";
    
$arg_names_array = explode(',', $arg_names);
    foreach(
$arg_names_array as $k => $v)
    {
        
$v = trim($v);
        echo 
"$v: [$arg_vals[$k]] ";
    }
    echo 
"\n";
}


//output:
//begin function [anyfunc]. $arg1: [val1] $arg2: [val2] $arg3: [val3]

?>
Oto Brglez 
6 years ago
How to create simple sum function that can sum N arguments. Like this:

<?php

function sum(){
    
$s=0;
    foreach(
func_get_args() as $a) $s+= is_numeric($a)?$a:0;
    return 
$s;
};

print 
sum(1,2,3,4,5,6); // will return 21
print sum(3,2,1); // will return 6
print sum(false,array(),5,5); // will return 10

?>
tristan dot colombo at laposte dot net 
7 years ago
In order to use the function 'func_get_args()' to instanciate differents type of objects, you must use the Reflection API.
By example, we have two different classes and we want to have an unique function (using an unfixed number of parameters) to create the objects. We create two classes 'a' and 'b' where constructors accept different numbers of arguments.
Class a (class/a.class.php):
<?php
  
include_once 'a.class.php';

  class 
b extends a
  
{
    private 
$param3;

    public function 
__construct($a, $b, $c)
    {
      
$this->param1 = $a;
      
$this->param2 = $b;
      
$this->param3 = $c;
    }

    public function 
display()
    {
      echo 
$this->param1 . ', ' . $this->param2 . ' and ' . $this->param3 . '!<br />';
    }
  }

?>

Class b (class/b.class.php):
<?php

  
class a
  
{
    private 
$param1;
    private 
$param2;

    public function 
__construct($a, $b)
    {
      
$this->param1 = $a;
      
$this->param2 = $b;
    }

    public function 
display()
    {
      echo 
$this->param1 . ' and ' . $this->param2 . '<br />';
    }
  }

?>

Main program :
<?php

  
function classFactory()
  {
    
// Retrieve arguments list
    
$_args = func_get_args();
    
// Delete the first argument which is the class name
    
$_className = array_shift($_args);

    
// Include the requested class
    
include_once 'class/' . $_className . '.class.php';

    
// Create Reflection object
    // See : http://www.php.net/manual/en/language.oop5.reflection.php
    
$_reflection = new ReflectionClass($_className);

    
// Use the Reflection API
    
return $_reflection->newInstanceArgs($_args);
  }

  
$a = classFactory('a', 'hello', 'world');
  
$b = classFactory('b', 'that\'s', 'all', 'folks');

  
$a->display();
  
$b->display();

?>
ario [a] mail [dot] utexas [dot] edu 
7 years ago
"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(1, 2, 3);

?>

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(1, 2, 3);

?>

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

"1, 2, 3"
bew 
9 years ago
A more concise way of expressing my idea from the previous post (I'd forgotten about array_slice()):

<?php
function func_get_default_args($a) {
    
$args = array_slice(func_get_args(), 1);
    return 
array_merge($args, array_slice($a, sizeof($args)));
}

function 
foo($a = 1, $b = 2, $c = 3) {
    
print_r(func_get_default_args(func_get_args(), $a, $b, $c));
}


// prints: Array ( [0] => a [1] => b [2] => 3 )
foo('a', 'b');
?>
volte6 at drunkduck dot com 
10 years ago
For those who have a use for a C style enum() function: 

<?php 
//******************************************* 
// void enum(); 
// enumerates constants for unique values guarenteed. 

function enum() 
{ 
  
$i=0; 
  
$ARG_ARR = func_get_args(); 
  if (
is_array($ARG_ARR)) 
  { 
    foreach (
$ARG_ARR as $CONSTANT) 
    { 
      
define ($CONSTANT, ++$i); 
    } 
  } 
} 


// USAGE: 
enum(ERR_USER_EXISTS, ERR_OLD_POST); 

// etc. etc. 
//******************************************* 

?> 


this can be used for error codes etc. 
I deliberately skipped the 0 (zero) define, which could be useful for error checking.
Anonymous 
13 years ago
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"; 
?>
dev at mp3addict dot pw 
1 year ago
This function is as printf() but only calls printf() when the debug_enabled global variable is set to TRUE. Because by this way, you can use debug() instead of echo | printf() and set debug_enabled to false after checking and submitting work into production.

cheers;

function debug () {
    if ($GLOBALS['debug_enabled'] == true) {
        $args        = func_get_args();
        $fmt         = $args[0];
        $printf_args = '';
        for ($i = 1; $i < count($args); $i++) {
            $printf_args .= ",\"{$args[$i]}\"";
        }
        eval("printf(\"{$fmt}\"{$printf_args});");
    }
}
mitko at edabg dot com 
6 years ago
<?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>
*/


?>
kangaroo232002 at yahoo dot co dot uk 
6 years ago
Instead of having to define your arg list twice, and keeping to the good style of initialising your variables in the head of your class, you can use (PHP5):

<?php
class myclass {
     public 
$value = null;
    public 
$key = null;
    public 
$column = null;
    public 
$table = null;
    public function 
__construct() {
        
$vars = get_class_vars();
        for(
$i=0; $i<func_num_args();$i++) {
            
$this->${$vars[$i]}=func_get_arg($i);
        }
    }
}

?>

which should allow you to set variables while retaining their default values if they are not set (in this case, null), without having to mess around with functions to retain default values so is much neater (just don't change the order you declare your vars!)

<?php
//usage
$c = new myclass("value", "tablekey", "tablecol", "table");
echo 
$c->key;
//prints 'tablekey'
?>
rafagd at gmail dot com 
8 years ago
Sometimes, you may need to dynamic set and get of args... 

This function merge array args, so you can dynamic set some args by sending an array arg. 

<?php 
  
function dynamicArgs(/*$arg1, $arg2...$argN*/) { 
    
$args = func_get_args(); $num  = func_num_args(); 
    for (
$i = 1; $i < $num; $i++) { 
      
$args[0] = array_merge((array) $args[0], (array) $args[$i]); 
    } 
    return 
$args[0]; 
  } 
  
  
var_dump(dynamicArgs('a',array('b','c'),'d',1); 
?> 

This should output like: 

array(5) { 
  [0]=> 
  string(1) "a" 
  [1]=> 
  string(1) "b" 
  [2]=> 
  string(1) "c" 
  [3]=> 
  string(1) "d" 
  [4]=> 
  int(1) 
}
来自 http://us3.php.net/func_get_args
普通分类: