func_get_args

Description

This function may be used in conjunction with
func_get_arg() and func_num_args()
to allow user-defined functions to accept variable-length argument lists.

Return Values

Returns an array in which each element is a copy of the corresponding
member of the current user-defined function's argument list.

Changelog

Version

Description

5.3.0

This function can now be used in parameter lists.

5.3.0

If this function is called from the outermost scope of a file
which has been included by calling include
or require from within a function in the
calling file, it now generates a warning and returns FALSE.

Errors/Exceptions

Generates a warning if called from outside of a user-defined function.

Notes

Note:

Because this function depends on the
current scope to determine parameter details, it cannot be used as a
function parameter in versions prior to 5.3.0. If this value must be passed, the results should be assigned
to a variable, and that variable should be passed.

Note:

If the arguments are passed by reference,
any changes to the arguments will be reflected in the values returned by this function.

Note:
This function returns a copy of the passed arguments only, and does not
account for default (non-passed) arguments.

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...

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):<?phpinclude_once 'a.class.php';

"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:

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.

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.

<?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 referenceis depreciated and produce warning.*/

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!)