====== Request for Comments: Short syntax for arrays ====== * Version: 0.9 * Author: Ryusuke Sekiyama , Sebastian Deutsch * Status: Implemented in 5.4 * First Published at: Jan 10, 2008 * Last update: May 9, 2011 **2008 Votes** * Votes (+9/-14): * Pro: Andrei Zmievski, Andi Gutmans, Pierre Joye, Rasmus Lerdorf, Stanislav Malyshev, Brian Moon, Kalle Sommer Nielsen, Edin Kadribasic, Philip Olson * Contra: Antony Dovgal, Derick Rethans, Jani Taskinen, Lokrain, Felipe Pena, Marcus Boerger, David Soria Parra, Johannes Schlüter, Maciek Sokolewicz, Ilia Alshanetsky, Daniel Brown, Jochem Maas, Hannes Magnusson, David Coallier * Userland Votes (+17/-3): * Pro: Sebastian Deutsch, Ryusuke Sekiyama, Stefan Marr, Alexey Zakhlestin, Carl P. Corliss, Darius Jahandarie, Giedrius D, Eric Coleman, Max Antonov, Mike Ford, Larry Garfield, Sam Barrow, Taylor Luk, Hans Ahlin, Karoly Negyesi, Guilherme Blanco, Jonathan-Bond Caron * Contra: Geoffrey Sneddon, Tomi Kaistila, David Zühlke * **Note: This list was created by guessing (cvs name vs. real name) who has commit rights - please correct mistakes** This RFC will discuss an language enhancement for simple and easy array definition. ===== Update: May 2011 ===== This topic has come up again on the list. ===== Introduction ===== In some cases it is necessary that you have to pass function parameters as an array. Especially when you work with nested arrays the php array function tends to make the code looking ugly. If you take a look at other languages (Ruby, Python, Javascript) there is a short syntax for arrays using square brackets. Maybe PHP should adopt this behaviour to make code more readable and maintainable. Another solution would be to implement [[http://en.wikipedia.org/wiki/Keyword_argument|named parameters]]. ===== Syntax ===== Ryusuke suggested two possibly syntaxes and also provides two patches for a solution: **Square bracket array shortcut - keys and values are separated by colons:** $a = [1, 2, 3]; $b = ['foo': 'orange', 'bar': 'apple', 'baz': 'lemon']; **Square bracket array shortcut - keys and values are separated by double arrows:** $a = [1, 2, 3]; $b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon']; ===== Proposal and Patch ===== The first solution is more compact: http://www.opendogs.org/pub/php-5.3dev-080109-sbar.patch \\ The second solution is more "The PHP Way": http://www.opendogs.org/pub/php-5.3dev-080109-sbar2.patch \\ ===== Pro / Contra ===== Pro and contra arguments that were derived from the list. ==== Pro ==== * Good for framework development when dealing with long parameterlists * Other web languages have similar syntax * Readable ==== Contra ==== * Yet another alias * Would take distinctness from [] * Not searchable through search engines * Unreadable * Patch may be difficult to maintain in future ===== Conclusion ===== This patch has been merged in PHP 5.4.0 and trunk. ===== Discussion on the List ===== - 2003-11-04 : [[http://markmail.org/message/rfolc6gxjmit6nes|Proposal: Array syntax]] - 2007-02-03 : [[http://markmail.org/message/67nff7mn4kqdbtfm|Syntactic improvement to array]] - 2008-01-08 : [[http://markmail.org/message/54sadarpmjdhsm3f|[RFC] Square brackets shortcut]] - 2008-01-11 : [[http://markmail.org/message/c4jwv7dl2uufxuxh|SUMMARY: Array syntax]] - 2008-05-21 : [[http://markmail.org/message/rsi4welftwou24p3|Short syntax for array literals]] - 2011-05-09 : [[http://markmail.org/message/fx3brcm4ekh645se|5.4 again]]