note 77922 deleted from function.func-get-args by danbrown

From: Date: Fri, 15 Jul 2011 17:46:40 +0000
Subject: note 77922 deleted from function.func-get-args by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181581@lists.php.net to get a copy of this message
Note Submitter: gerry+phpnet at buzka dot com ---- 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) { // Grab the list of expected arguments. return func_get_args(); } function func_get_default_args($a) { // Grab the list of expected arguments. $args = array_slice(func_get_args(), 1); // Append any unexpected agruments that may have been also passed // to the function, but were not in the param list. 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'); /* Output: expected args: Array ( [0] => 20 [1] => 3 ) all args: Array ( [0] => 20 [1] => 3 ) expected args: Array ( [0] => 20 [1] => 95 ) all args: Array ( [0] => 20 [1] => 95 [2] => unexpected [3] => variables ) */ ?>

« previous php.notes (#181581) next »