|
|
func_get_args (PHP 4, PHP 5) func_get_args --
Возвращает массив аргументов функции
Описаниеarray func_get_args ( void )
Возвращает массив, в котором каждый элемент является соответствующим
членом списка аргументов пользовательской функции.
func_get_args() генерирует предупреждение
при вызове вне определения функции.
func_get_args() может быть использована совместно с
func_num_args() и func_get_arg()
для создания функций с переменным количеством аргументов.
tristan dot colombo at laposte dot net
18-Oct-2007 01:03
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()
{
$_args = func_get_args();
$_className = array_shift($_args);
include_once 'class/' . $_className . '.class.php';
$_reflection = new ReflectionClass($_className);
return $_reflection->newInstanceArgs($_args);
}
$a = classFactory('a', 'hello', 'world');
$b = classFactory('b', 'that\'s', 'all', 'folks');
$a->display();
$b->display();
?>
gerry+phpnet at buzka dot com
19-Sep-2007 07:40
If you were having trouble understanding the func_get_default_args() function below, this example should make things more clear. The author of the function could have written it like func_get_expected_default_args() but that doesn't account for args which were passed but not specified in the param list.
example:
<?php
function func_get_expected_default_args($a) {
return func_get_args();
}
function func_get_default_args($a) {
$args = array_slice(func_get_args(), 1);
return array_merge($args, array_slice($a, count($args)));
}
function foo($bar=5, $foobar=3){
$args = func_get_expected_default_args2($bar, $foobar);
echo 'expected args: '. print_r($args, true);
$args = func_get_default_args(func_get_args(), $bar, $foobar);
echo 'all args: '. print_r($args, true);
}
foo(20);
foo(20, 95, 'unexpected', 'variables');
?>
Sinured
20-Aug-2007 05:38
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.
ario [a] mail [dot] utexas [dot] edu
07-May-2007 02:50
"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"
jmcguire81 [at] gmail.com
21-Mar-2007 03:13
Here is another variation on accepting a variable number of arguments. This allows for a variable number of arguments to be passed to a Class constructor, as well as a customized class version to be used dynamically. Syntax in code is:
$mail = Generator("MailClassName", $db_ref);
function Generator() {
$numargs = func_num_args();
$classname = func_get_arg(0);
$argstring='';
if ($numargs > 1) {
$arg_list = func_get_args();
for ($x=1; $x<$numargs; $x++) {
$argstring .= '$arg_list['.$x.']';
if ($x != $numargs-1) $argstring .= ',';
}
}
if (class_exists("Custom{$classname}")) {
$classname = "Custom{$classname}";
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
Hope this is of use to someone.
rafagd at gmail dot com
13-Feb-2007 08:53
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.
<?
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)
}
31-Oct-2006 10:26
Same idea as below, but this:
foreach( $args as $k => $v ){
switch($k){
case 'a':
$this->a= $v; break;
case 'b':
$this->b= $v; break;
case 'c':
$this->c= $v; break;
}
}
can be shortened to this (as long as all public variables have default values set in their declarations):
foreach( $args as $k=>$v)
if(isset($this->$k)) $this->$k = $v;
kidekat
01-Oct-2006 06:02
To pass named arguments in a Perl fashion through class constructors, I use this:
<?php
class Test{
public $a = 0;
public $b = 'string';
public $c = array();
public function __construct( $args = array() ) {
foreach( $args as $k => $v ){
switch($k){
case 'a':
$this->a= $v; break;
case 'b':
$this->b= $v; break;
case 'c':
$this->c= $v; break;
}
}
}
}
$t = new Test( array( 'b'=>'new value', 'c'=>array(1,'test') ) );
?>
This allows $a to keep its default of 0, while $b gets reassigned to 'new value' and $c becomes the array(1,'test'). The catch is that you add O(n^2) "big-O notation" to the begging of every class constructor which becomes expensive on "larger" classes. While arguments defaults like the following have only O(n) "constant" amount of work.
<?php
public funciton __construct( $a=0, $b='string', $c=array()){ ... }
?>
bew
31-Mar-2006 07:55
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));
}
foo('a', 'b');
?>
Nathan Ostgard
06-Dec-2005 10:14
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.
robert at defore dot st
15-Feb-2005 12: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'
T.M.
04-Nov-2004 07: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 02: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.
mark at manngo dot net
23-Mar-2003 11: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");
fbeyer at clickhand dot de dot noSpamPlease
19-Jan-2002 08: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
17-Sep-2001 11: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 07: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.
<?
// 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";
?>
|