php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12712 named function parameters
Submitted: 2001-08-13 04:05 UTC Modified: 2011-08-05 08:23 UTC
Votes:6
Avg. Score:4.2 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: martin at semester dot sk Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martin at semester dot sk
New email:
PHP Version: OS:

 

 [2001-08-13 04:05 UTC] martin at semester dot sk
Good day. It is not a really bug report but a little suggestion for new PHP relase. I'll be good thing to add to PHP
language ability to create functions with named parameters like in Visual Basic. For example:

//Classic function:
function printhello($name, $bold = false, $italic = false)
{
	$ret = "Hello $name!"
	if ($bold)
		$ret = "<strong>$ret</strong>";
	if ($italic)
		$ret = "<em>$ret</em>";
	echo $ret;
}

//Suggested function:
function printhello(name: $name, fat: $bold = false, emphased: $italic = false)
{
	// same body as above ...
}

This new function could be called like: printhello("Martin", true);
or like new: printhello(fat: true, name: "Martin");

Thank you.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-27 08:37 UTC] coder at muctr dot edu dot ru
Would like to add the reason.
In medium to big projects (when many developers are involved) this feature could be of a great help. Because the code could be written in self-documenting manner.

E.g. compare
  $html = formatSomeData( $data, true, false, true, 10 );
to
  $html = formatSomeData( entity_x: $data, italic: true, bold: false, monospace: true, fontSize: 10 );

In the second case one doesn't have to search for formatSomeData() definition to start using it.
 [2010-12-13 18:18 UTC] giecrilj at stegny dot 2a dot pl
There is no need to change the function declaration.  
Consider this syntax:
  printhello (bold => true, name => 'Martin');
and let the compiler translate it to 
  printhello ('Martin', true)
given the traditional declaration.
 [2010-12-17 12:38 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Windows 2000 +Operating System: * -PHP Version: 4.0.6 +PHP Version: *
 [2011-05-19 16:46 UTC] briank at kappacs dot com
I would like to suggest the following syntax:

function f ($un1, $un2, $un3 = 'default',
 'hello' => 'goodbye', 'planet' => 'earth') {
    # $_FUNCARGS is a magic local short-hand for func_get_args()
    var_dump($_FUNCARGS);
}

When called as:

f('one', 'named' => 'param', 'two', 5 => 'works', 'hello' => 'world', 'too');

... the equivalent of this would happen behind the scenes:

# (The order and position of the *named* parameters here are just
# an example and not necessarily what PHP might implement)
$_FUNCARGS = array(0 => 'one', 1 => 'two', 2 => 'default', 5 => 'works',
 6 => 'too', 'named' => 'param', 'hello' => 'world', 'planet' => 'earth');
$un1 = &$_FUNCARGS[0];
$un2 = &$_FUNCARGS[1];
$un3 = &$_FUNCARGS[2];

The bottom line is that the parameters look like an array(), with
both numeric and string keys, in any order, just like an array.

For readability, developers should probably put the unnamed parameters
before the named parameters just as a matter of style (for both function
calls and declarations), but the PHP engine should take them as they come.
 [2011-08-05 08:23 UTC] tacker@php.net
-Status: Open +Status: Wont fix
 [2011-08-05 08:23 UTC] tacker@php.net
See #22216

helly@php.net said:
This has been discussed and declined several times. Check the mail archives for 
discussions.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC