php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #22216 Named Arguments
Submitted: 2003-02-13 16:59 UTC Modified: 2007-11-30 11:12 UTC
Votes:18
Avg. Score:4.6 ± 0.7
Reproduced:18 of 18 (100.0%)
Same Version:12 (66.7%)
Same OS:14 (77.8%)
From: tim dot lokot at printsoft dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tim dot lokot at printsoft dot com
New email:
PHP Version: OS:

 

 [2003-02-13 16:59 UTC] 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:="bugs@php.net", 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?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-14 11:48 UTC] jason at godsey dot net
<?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")));

?>
 [2007-11-09 17:52 UTC] zyss at mail dot zp dot ua
It is really needef feature to have in PHP. I miss this feature very much when function has many arguments or when there are several boolean arguments that require true/false values or when there are several arguments with default value set and you want to use only, say, last argument it would be very helpful to have named arguments support. Having this feature would dramatically increase code readability.

Even standard functions will be easier to read having this feature. for example:

  in_array($needle, $haystack, $strict => true);

is much easier to read that just

  in_array($needle, $haystack, true);

It would be more native to PHP to use named arguments with => like
  foo(var2 => $value);
or
  foo($var2 => $value);
 [2007-11-30 11:12 UTC] helly@php.net
This has been discussed and declined several times. Check the mail archives for discussions.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 01:01:28 2024 UTC