note 63758 added to function.func-get-args

From: Date: Thu, 30 Mar 2006 05:48:33 +0000
Subject: note 63758 added to function.func-get-args
Groups: php.notes 
Request: Send a blank email to php-notes+get-106992@lists.php.net to get a copy of this message
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') ---- Server IP: 64.71.164.2 Probable Submitter: 68.59.225.167 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.func-get-args.php Edit -- http://master.php.net/note/edit/63758 Del: integrated -- http://master.php.net/note/delete/63758/integrated Del: useless -- http://master.php.net/note/delete/63758/useless Del: bad code -- http://master.php.net/note/delete/63758/bad+code Del: spam -- http://master.php.net/note/delete/63758/spam Del: non-english -- http://master.php.net/note/delete/63758/non-english Del: in docs -- http://master.php.net/note/delete/63758/in+docs Del: other reasons-- http://master.php.net/note/delete/63758 Reject -- http://master.php.net/note/reject/63758 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106992) next »