rfc:bare_name_array_literal

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:bare_name_array_literal [2014/06/01 22:51] – Added comments with var_dump for further clarity ajfrfc:bare_name_array_literal [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2014-06-01   * Date: 2014-06-01
   * Author: Andrea Faulds <ajf@ajf.me>   * Author: Andrea Faulds <ajf@ajf.me>
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/bare_name_array_literal   * First Published at: http://wiki.php.net/rfc/bare_name_array_literal
  
Line 57: Line 57:
  
 This means that only 2 characters need to be typed (3 with spaces) for most key names. It makes defining arrays with string keys more convenient and it's easier to read (especially if you have an IDE which syntax highlights strings). This means that only 2 characters need to be typed (3 with spaces) for most key names. It makes defining arrays with string keys more convenient and it's easier to read (especially if you have an IDE which syntax highlights strings).
 +
 +A secondary benefit is to the [[rfc:named_params|Named Parameters]] RFC in that it would provide a clear syntax choice consistent with arrays and the same as C# and Objective-C, i.e.:
 +
 +<code php>
 +$array = [true, 3, foo: 'bar', "foo bar" => 7];
 +call_user_func_array('quxbang', $array);
 +// is the same as:
 +quxbang(...$array):
 +// is the same as:
 +quxbang(true, 3, foo: 'bar', "foo bar" => 7);
 +</code>
 +
 +Symmetry between arrays and named parameters would be desirable as otherwise it could cause confusion. This RFC would ensure it.
 +
 +Even without named parameters, this makes it nicer to use functions which take an array of options. For example:
 +
 +<code php>
 +$ctx = stream_context_create([http: [method: "GET", header: "Accept-language: en", user_agent: 'Foo-Bot', protocol_version: 1.1]]);
 +
 +// or
 +
 +$hash = password_hash($_POST['pwd'], PASSWORD_BCRYPT, [salt: $secure_salt, cost: 25]);
 +</code>
 +
 +This makes creating JSON more convenient, too:
 +
 +<code php>
 +// Real example
 +$ws->send(json_encode([
 +    type: 'join',
 +    room: $player->currentRoom,
 +    nick: $player->curretName,
 +    x: 0,
 +    y: 0,
 +    avatar: 'foo.png'
 +]));
 +</code>
  
 This complements the [[rfc:bare_name_array_dereference|Bare Name Array Dereference]] RFC, as both this proposal and that one deal with using bare T_STRING names for array keys. This complements the [[rfc:bare_name_array_dereference|Bare Name Array Dereference]] RFC, as both this proposal and that one deal with using bare T_STRING names for array keys.
Line 82: Line 119:
  
 <code php> <code php>
-const('foo', 'bar');+const foo 'bar';
 $arr = [foo: 3]; $arr = [foo: 3];
 // array(1) { // array(1) {
Line 101: Line 138:
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-None. This will not affect existing code.+None. This will not affect existing code. => will  continue to resolve constants. : will not.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 110: Line 147:
  
 SAPIs, extensions, opcache, constants and php.ini are unaffected. No new opcodes are added, this is purely a parsing change. SAPIs, extensions, opcache, constants and php.ini are unaffected. No new opcodes are added, this is purely a parsing change.
 +
 +As aforementioned, it would give a better syntax for the [[rfc:named_params|Named Parameters]] RFC.
  
 ===== Open Issues ===== ===== Open Issues =====
Line 123: Line 162:
 Object literals might be a nice-to-have, but then again (object)[foobar: 3] would work just as well. Object literals might be a nice-to-have, but then again (object)[foobar: 3] would work just as well.
  
-===== Proposed Voting Choices =====+===== Vote =====
  
-A 2/3 majority as it is a language change. It will be straight Yes/No vote.+A 2/3 majority as it is a language change. Voting began on 2014-06-21 (after hiccup it was restarted) and ended 2014-06-28.  
 +<doodle title="Merge bare name array literal into master/PHP 5.7?" auth="user" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 142: Line 185:
 ===== References ===== ===== References =====
  
-* http://phpsadness.com/sad/45 - one of my inspirations for this+  * The [[rfc:named_params|Named Parameters]] RFC is one reason for this 
 +  * http://phpsadness.com/sad/45 - one of my inspirations for this 
 +  * The [[rfc:bare_name_array_dereference|Bare Name Array Dereference]] RFC goes hand-in-hand with this one 
 +  * http://marc.info/?t=140164578600002&r=1&w=2 - mailing list discussion on this RFC (it was originally a single RFC containing both this and the dereferencing proposal, but was split into two)
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
 None as yet. None as yet.
rfc/bare_name_array_literal.1401663086.txt.gz · Last modified: 2017/09/22 13:28 (external edit)