ID: 22216 Comment by: jason at godsey dot net Reported By: tim dot lokot at printsoft dot com Status: Open Bug Type: Feature/Change Request Operating System: All PHP Version: 4.3.0 New Comment:
<?php // Missing named arguments work around.. // Set the default/REQUIRED var to something unlikely. define("REQUIRED", "_^_-" . rand(1000000,99999999) . "-" . time()); function parseRequired (&$defaults, &$args) { foreach ($defaults as $key=>$value) { if(!isset($args["$key"])) { if ($value == REQUIRED) { $backtrace = debug_backtrace(); $function = $backtrace[1]["function"]; throw new Exception("function: $function var: \$$key not defined"); } $args[$key] = $value; } } return 0; } function debugging ($args) { $defaults = array( "name"=>"Lanny Jason Godsey", "text"=>"This is the default text!", "date"=>REQUIRED ); parseRequired($defaults, $args); print "($args[date]) Welcome $args[name], text entered: $args[text]\n"; } debugging(array("name"=>"L. Jason Godsey","date"=>date("Y-m-d"))); ?> Previous Comments: ------------------------------------------------------------------------ [2003-02-13 16:59:47] tim dot lokot at printsoft dot com I know this can be accomplished in other way and also know this has been brought up before, but they are messy and require code from the developer to handle this which seems to go against the php ethos of easy and fast to develop. If named arguments could be passed to functions and actually processed by the php parser, then this would be really great. Especially where function prototypes have default values in them and you only want to set one of them. The proposed syntax would be something like this: function foo ($var1="some value", $var2, $var3="some other value") { // function code } then to call the function would be like this foo (var2:="value for argument 2 only"); This would also be great if this feature would extend to the existing functions in the extensions as well. mail (to:="[EMAIL PROTECTED]", message:="no message", subject:="subject"); This in my opinion makes the function calls more readable in some circumstances, particularly when you have to keep going back to the prototype to figure out what the order of the arguments is. It's kind of self documenting really when you look at code written in this way. Bug #2285: default arguments skipping (http://bugs.php.net/bug.php?id=2285) was filed with a similar suggestion and the solution suggested was to use associative arrays. Becuase it was never explained in any of the other reports as to why this wasn't going to be implemented, why the above not more preferable to have than always have to implement the same code this: function foo ($args) { // Named Argument Checks $var1 = "some value"; $var3 = "some other value"; foreach ($args as $key => $value) { $$key = $value; } // Do function code here } foo (array ("var2"=>"some other value again","var1"=>"variable 1")); Surely forcing developers into using this messy syntax goes against one of the main strengths of php which is simple code that's easy to read, understand and develop. I'm in no wasy saying the above code was hard, just the first example of named arguments seems to fit more into the php way than the second example. If this is to be rejected like the other requests for it, can you please provide a reason why the array method is more preferable? ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=22216&edit=1