|
|
 |
func_get_args (PHP 4, PHP 5) func_get_args --
Returns an array comprising a function's argument list
Descriptionarray func_get_args ( void )
Returns an array in which each element is a copy of the corresponding
member of the current user-defined function's argument
list. func_get_args() will generate a warning
if called from outside of a function definition.
This function cannot be used directly as a function parameter. Instead,
its result may be assigned to a variable, which can then be passed to
the function.
Note:
This function returns a copy of the passed arguments only, and does not
account for default (non-passed) arguments.
func_get_args() may be used in conjunction
with func_num_args() and
func_get_arg() to allow user-defined functions
to accept variable-length argument lists.
User Contributed Notes
func_get_args
N3Y
13-May-2005 02:37
The the average of ints and mixed numbers..
<?php
function get_average() {
$numargs += func_num_args();
foreach (func_get_args() as $k => $v) {
if (is_array($v)) {
$numargs += count($v)-1;
foreach ($v as $kk => $vv) {
$num += floatval($vv);
}
} else {
$num += floatval($v);
}
}
return "$num / $numargs = ".round($num/$numargs,4);
}
echo get_average(array(1,2,3,4),5,4,3);
?>
berndt at www dot michael - berndt dot de
03-May-2005 08:59
theodore at websitters dot com
18-Apr-2005 12:38
Actually, that last example doesn't work fully, since without $i explicitly defined as starting at 0, it instead starts at 1 (for whatever reason). So the first argument is never processed, leading to serious problems. A better version is as follows:
<?php
function mutator($args) {
$n = count($args);
for ($i = 0; $i < $n; $i++) $args[$i] = 'howdy';
}
$a = "hello";
$b = "strange";
$c = "world";
mutator(array(&$a, $b, $c));
echo "$a $b $c";
?>
robert at defore dot st
15-Feb-2005 02:47
# Another attempt at named args (perl-inspired):
# list_to_assoc('key', 'value', 'key', 'value', ...) =>
# pairs[]
function list_to_assoc() {
$list = func_get_args();
$assoc = array();
while ($list and count($list) > 1) {
$assoc[array_shift($list)] = array_shift($list);
}
if ($list) { $assoc[] = $list[0]; }
return $assoc;
}
# Usage:
function example($required) {
$args = func_get_args(); array_shift($args); # drop 'required'
$rest = list_to_assoc($args);
echo "$required\n" . $rest['comment'];
}
example("This is required...",
'comment', 'this is not.'); # this is like 'comment' => 'this is not'
intuz ät gmx döt de
08-Nov-2004 03:09
An updated LoadClass / LoadModule function.
This function can be called with all PHP Values e.g. Arrays or Objects.
<?php
class SimpleClassLoader {
var $Module;
SimpleClassLoader() {
$this->Module = NULL;
}
function &LoadModule($incpath="inc/",$prefix="",$instance) {
if(!isset($this->Module[$instance])) {
$numargs = func_num_args();
if( $numargs > 3 ) {
$arg_list = func_get_args();
for($i=3;$i<count($arg_list);$i++) {
$parameter .= "\$arg_list[$i]";
if($i != (count($arg_list)-1)) $parameter .= ",";
}
}
if( !class_exists( $instance ) ) {
if( $err = include_once($incpath.$prefix.'class.'.$instance.'.php') ) {
define("_CLASS_".strtoupper($instance),TRUE);
eval("\$this->Module[\$instance] = new \$instance($parameter);");
} else {
exit('Error! could not include: '.$incpath.$prefix.'class.'.$instance.'.php'.$err);
}
} else {
eval("\$this->Module[\$instance] = new \$instance($parameter);");
}
}
return $this->Module[$instance];
}
}
$MyObj = new SimpleClassLoader();
$MyObj->LoadModule("classes/","","MySql","localhost","...");
?>
Hope my Source is correct..
Hope it helps somebody.. :)
T.M.
04-Nov-2004 09:24
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); ?>
volte6 at drunkduck dot com
30-Sep-2004 04:54
For those who have a use for a C style enum() function:
//*******************************************
// 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.
tightcode_nosp@m_hotmail
15-Sep-2004 11:45
It is possible to use func_get_args() as a parameter!
In addressing "nutbar at innocent dot com" post on the 2003-03-12 @ 09:27, it is worth noting that there is a work-around.
For people receiving this error:
Fatal error: func_get_args(): Can't be used as a function parameter
The solution is to make sure that func_get_args() is the first function parameter. If it is the second, third, fourth, etc... it will give that error. However if it is the first or the only parameter, it is handled as you would expect.
example:
<?php
function working_example($arg1,$arg2) {
return reformat_func_get_args(func_get_args(),"Working Function");
}
function failing_example($arg1,$arg2) {
return reformat_func_get_args("Failing Function",func_get_args());
}
function reformat_func_get_args($arg1,$arg2) {
if (is_array($arg1)) {
return $arg2.":".join(",",$arg1);
}
elseif (is_array($arg2)) {
return $arg1.":".join(",",$arg2);
}
else {
return false;
}
}
echo working_example("apples","oranges");
echo failing_example("apples","oranges");
?>
I hope this helps someone. It took me a bit to figure out a way of making it work.
Cheers,
TightCode
flobee at gmx dot net
25-Apr-2004 10:34
!! to my last post, there is an error! i have not tested if the vars will be "ported" to the constructor in that way, so delete the for statement and use an array to bring all vars to the constructor:
<?php
if( $numargs > 1 ) {
$args = func_get_args();
}
?>
flobee at gmx dot net
23-Apr-2004 12:53
re.: avenger at php (#) net #s post ->
here is a litte tool to load/get objects on request (saves a lot of power and inclusions requests) (seems that the __autoload of php5 will do this)
<?php
function &LoadClass($instance) {
static $loadedClasses = NULL;
$numargs = func_num_args();
if( $numargs > 1 ) {
$arg_list = func_get_args();
for($i=1;$i<$numargs;$i++) {
if($i+1 == $numargs) {
$args .= $arg_list[$i];
} else {
$args .= $arg_list[$i] .', ';
}
}
}
if( !class_exists( $instance ) ) {
if( $err = include_once('./lib/class.'. $instance .'.php') ) {
define("_CLASS_".strtoupper($instance),TRUE);
$loadedClasses[$instance] = new $instance($args);
} else {
exit('Error! could not include: ./lib/class.'.$instance.'.php'. $err);
}
} else {
$loadedClasses[$instance] = new $instance($args);
}
return $loadedClasses[$instance];
}
$myObject = & LoadClass('MyTest', $var1='hello',$var2='php');
?>
mark at manngo dot net
24-Mar-2003 01:13
You can also fake named arguments using eval:
function test()
{ foreach (func_get_args() as $k=>$arg) eval ("\$$arg;");
echo "$a plus $b gives ".($a+$b);
}
test("a=3","b=4");
nutbar at innocent dot com
12-Mar-2003 02:27
Apparently PHP won't let you pass func_get_args() as a paramater to another function, such as call_user_func_array().
Not sure why, but I guess it doesn't hurt *too* much to just assign its value to a variable.
By the way, using this and other functions, lets you get one step closer to making easy function macros:
function my_printf() {
$args = func_get_args();
return call_user_func_array('printf', $args);
}
Now you can call my_printf() just as if it were printf() itself.
avenger at php dot net
17-Feb-2003 09:32
I used this function make my classload function,look:
<?php
$classPath = "class/";
function loadclass() {
global $classPath;
$args = func_get_args();
foreach($args as $className) {
$className = strtolower($className);
if (file_exists("$classPath$className.php")) {
include_once("$classPath$className.php");
}
}
}
?>
fbeyer at clickhand dot de dot noSpamPlease
19-Jan-2002 10:02
Another way of passing references with a dynamic number of arguments: (This example is limited to 10 arguments)
<?php
define('NULL_ARG', 'DUMMY_ARGUMENT');
function refArg($arg0 = NULL_ARG,
$arg1 = NULL_ARG,
$arg2 = NULL_ARG,
$arg3 = NULL_ARG,
$arg4 = NULL_ARG,
$arg5 = NULL_ARG,
$arg6 = NULL_ARG,
$arg7 = NULL_ARG,
$arg8 = NULL_ARG,
$arg9 = NULL_ARG)
{
for ($args=array(), $i=0; $i < 10; $i++) {
$name = 'arg' . $i;
if ($i < func_num_args()) {
$args[$i] = &$$name;
}
unset($$name, $name);
}
$args[0] = 'Modified.';
}
$test = 'Not modified.<br>';
refArg(&$test);
echo $test; ?>
daveNO at ovumSPAMdesign dot com
18-Sep-2001 01:29
<?php
function varargs($args) {
$count = count($args);
for ($i = 0; $i < $count; $i += 2) {
$result[$args[$i]] = $args[$i + 1];
}
return $result;
}
function test(&$ref1, &$ref2) {
$foo = "oof";
extract(varargs(func_get_args()));
echo nl2br("\n\$var1 = $var1");
echo nl2br("\n\$var2 = $var2");
echo nl2br("\n\$foo = $foo\n\n");
$ref1 = 42;
$ref2 = 84;
}
$a = 5;
$b = 6;
echo nl2br("Before calling test(): \$a = $a\n");
echo nl2br("Before calling test(): \$b = $b\n");
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");
?>
04-Jun-2001 09:44
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.
<?
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";
?>
| |