Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Last active December 22, 2015 02:39
Show Gist options
  • Save philsturgeon/6405087 to your computer and use it in GitHub Desktop.
Save philsturgeon/6405087 to your computer and use it in GitHub Desktop.
PHP Named Param History
For do_something(parameter => 23); we have the benefit, that IDEs could
easily validate that which prevents typos and provide aid. But then I'm
not sure this fits to the language either as we always expect quoted
names. And from the some distance this looks like a constant used used
as parameter name, as in
<?php
const foo = 42;
$a = array(foo => 'bar');
do_something(foo => 'bar');
?>
Another possible limitation there is that our parser won't allow
keywords there. An example where this might be annoying might be
something along these lines:
<?php
function getInstance($param1 = 1, $param2 = 2, $class = 'default_class' {
$r = new ReflectionClass($class);
return $r->createInstance($param1, $param2);
}
getInstance(class => 'my_class');
?>
Just some random thoughts, maybe somebody has a better syntax, maybe
that's just me,
johannes
http://marc.info/?l=php-internals&m=127042255029314&w=4
2001 php-general - user asks, gets told to use arrays
http://grokbase.com/t/php/php-general/019s1t2bnp/feature
2001 bug report - wanted to change declaration
https://bugs.php.net/bug.php?id=12712
2003 bug report - weird syntax foo (var2:="value for argument 2 only");
https://bugs.php.net/bug.php?id=22216
- 2007 the response from [email protected] was "nope, check the archives"
2005 somebody in paris said "no"
http://www.php.net/~derick/meeting-notes.html#named-parameters
2010 April Rasmus says he's not against it
http://grokbase.com/p/php/php-internals/1043r677w0/php-dev-named-parameters
2010 October Attempted patch
http://grokbase.com/t/php/php-internals/10afa5696w/named-parameters
2010 Stanislav outlines implementation difficulties
http://marc.info/?l=php-internals&m=127041892126722&w=4

Example A

Inside the function declaration all values are assigned to variables, so using variable syntax on the outside seems to make sense too.

$api->getFriends($screen_name = 'phpdrama', $include_user_entities = true);

Pro:

  • Consistent with function declaration

Con:

  • This will resolve in current versions of PHP, meaning potential BC issues.

Example B

Inside the function declaration all values are assigned to variables, so using variable syntax on the outside seems to make sense too.

$api->getFriends($screen_name => 'phpdrama', $include_user_entities => true);

Pro:

  • Use of $ in variables to keep things consistent.
  • Does not compile in current syntax, so no BC issues.

Con:

  • This potentially looks like the named parameter should be dynamically referenced with the value of the variable.

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) ...

Example C

Avoid using $ when referencing the function.

$api->getFriends(screen_name => 'phpdrama', include_user_entities => true);

Pro:

  • Does not compile in current syntax, so no BC issues.
  • Thick arrow is consistent with array usage, which is similar to what this is.

Con:

  • This potentially looks like the named parameter should be dynamically referenced with the value of a constant.
  • The parser will not allow keywords here, so class='foo' would throw an error.

Example D

Another approach at avoiding the $ symbol.

$api->getFriends(screen_name='phpdrama', include_user_entities=true);

Pro:

  • Does not compile in current syntax, so no BC issues.
  • = is consistent with the assignment of default values in the function signature.

Con:

  • Looks like it is trying to define a constant perhaps(?)
  • The parser will not allow keywords here, so class='foo' would throw an error.

Example E

PDO style named params.

$api->getFriends(:screen_name => 'phpdrama', :include_user_entities => true);

Pro:

  • Does not compile in current syntax, so no BC issues.
  • Similar to the PDO named parameter solution so not totally alien.
  • Uses the same syntax as Ruby.

Con:

  • Adds a new symbol : which is currently not used.

Example F

$api->getFriends(screen_name: 'phpdrama', include_user_entities: true);

Pro:

  • Does not compile in current syntax, so no BC issues.
  • Does not look like assignment to variable or definition of constant.

Con:

  • Parser issues with keywords?

Example G

$api->getFriends('screen_name' => 'phpdrama', 'include_user_entities' => true);

Pro:

  • Does not compile in current syntax, so no BC issues.
  • Unambiguous to the parser.
  • Looks like an array.

Con:

  • Looks like an array.
@bwoebi
Copy link

bwoebi commented Sep 12, 2013

At some points is noted that The parser will not allow keywords here, so class='foo' would throw an error. This would be actually become a non-issue if php/php-src#438 gets merged.

@arnold-almeida
Copy link

E++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment