note 83566 deleted from function.func-get-args by danbrown
| From: | danbrown@php.net | Date: | Fri, 15 Jul 2011 17:46:21 +0000 |
| Subject: | note 83566 deleted from function.func-get-args by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181580@lists.php.net to get a copy of this message | ||
Note Submitter: kentfredric at gmail dot com
----
Re This Note:
Note: Because this function depends on the current scope to determine parameter details, it cannot
be used as a function parameter. If this value must be passed, the results should be assigned to a
variable, and that variable should be passed.
And In reply to ario [a] mail [dot] utexas [dot] edu
It would appear the code posted by ario is now in fact valid. ( 5.2.6-pl1-gentoo )
And in fact, the following code works perfectly.
<?php
function foo( $a, $b=null , $c=null )
{
var_export(array($a,$b,$c));
}
function bar()
{
foo( func_get_args() );
}
bar( 4,1,2 );
?>
and so does
<?php
foo( func_get_args() , 1, 2, 3);
?>
It seems the magic is bound to position 1 to let it through.
<?php
function get($a){ return $a; }
function foo(){
var_dump('bar', func_get_args()); // not works
var_dump('bar', get( func_get_args()) ); // not this either
var_dump(func_get_args(),'bar' ); // this works
var_dump(get(func_get_args()),'bar'); // as does this.
}?>
Cool trick that results.
<?php
function cufa( $args, $func ){ return call_user_func_array( $func, $args ); }
function bar( $a, $b, $c,$d )
{
var_dump( $a, $b, $c, $d );
}
function foo(){ return cufa(func_get_args(), 'bar' ); }
function baz(){ return cufa(array_merge(func_get_args(),array('baz')), 'bar' );
}
?>
foo( 1,2,3 );
int(1)
int(2)
int(3)
NULL
baz( 3,2,1 );
int(3)
int(2)
int(1)
string(3) "baz"