note 63758 deleted from function.func-get-args by bjori

From: Date: Tue, 25 Apr 2006 19:04:37 +0000
Subject: note 63758 deleted from function.func-get-args by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-109665@lists.php.net to get a copy of this message
Note Submitter: bew ---- func_get_args() "does not account for default (non-passed) arguments". Here is a workaround: // this one doesn't work the way you'd like it to function foo($a = 1, $b = 2, $c = 3) { $args = func_get_args(); print_r($args); } // only prints array([0] => first) foo('first'); // this one does work right, but you have some mess function bar($a = 1, $b = 2, $c = 3) { $args = func_get_args(); array_splice($args, 0, 3); array_unshift($args, $a, $b, $c); print_r($args); } // prints array([0] => first [1] => 2 [2] => 3) bar('first') // you could call a function like this so you don't have to // mess with array splicing and shifting in each function function func_get_default_args($a) { $args = func_get_args(); array_splice($args, 0, 1); array_splice($a, 0, sizeof($args)); return array_merge($args, $a); } function foobar($a = 1, $b = 2, $c = 3) { $args = func_get_default_args(func_get_args(), $a, $b, $c); print_r($args); } // prints array([0] => first [1] => 2 [2] => 3) foobar('first')

« previous php.notes (#109665) next »