note 77922 added to function.func-get-args

From: Date: Thu, 20 Sep 2007 02:40:59 +0000
Subject: note 77922 added to function.func-get-args
Groups: php.notes 
Request: Send a blank email to php-notes+get-130616@lists.php.net to get a copy of this message
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 ) */ ?> ---- Server IP: 203.16.234.19 Probable Submitter: 203.59.28.173 ---- Manual Page -- http://www.php.net/manual/en/function.func-get-args.php Edit -- https://master.php.net/note/edit/77922 Del: integrated -- https://master.php.net/note/delete/77922/integrated Del: useless -- https://master.php.net/note/delete/77922/useless Del: bad code -- https://master.php.net/note/delete/77922/bad+code Del: spam -- https://master.php.net/note/delete/77922/spam Del: non-english -- https://master.php.net/note/delete/77922/non-english Del: in docs -- https://master.php.net/note/delete/77922/in+docs Del: other reasons-- https://master.php.net/note/delete/77922 Reject -- https://master.php.net/note/reject/77922 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130616) next »