rfc:shortsyntaxforarrays

This is an old revision of the document!


Request for Comments: Short syntax for arrays

  • Version: 0.5
  • Date: 2008-27-05
  • Last update: 2008-30-05
  • Author: Ryusuke Sekiyama <rsky0711 at gmail . com>, Sebastian Deutsch <sebastian.deutsch at 9elements . com>
  • Status: Rejected
  • First Published at: 2008-01-10
  • Votes (+5/-9):
    • Pro: Andrei Zmievski, Andi Gutmans, Pierre Joye, Rasmus Lerdorf, Stanislav Malyshev
    • Contra: Antony Dovgal, Derick Rethans, Jani Taskinen, Lokrain, Felipe Pena, Lukas Kahwe Smith, Marcus Boerger, David Soria Parra, Johannes Schlüter
  • Userland Votes (+20/-10):
    • Pro: Sebastian Deutsch, Ryusuke Sekiyama, Stefan Marr, Alexey Zakhlestin, Brian Moon, Carl P. Corliss, Darius Jahandarie, Giedrius D, Eric Coleman, Kalle Sommer Nielsen, Max Antonov, Mike Ford, Larry Garfield, Sam Barrow, Taylor Luk, Hans Ahlin, Karoly Negyesi, Edin Kadribasic, Guilherme Blanco, Jonathan-Bond Caron
    • Contra: David Coallier, Geoffrey Sneddon, Hannes Magnusson, Tomi Kaistila, David Zühlke, Maciek Sokolewicz, Philip Olson, Ilia Alshanetsky, Daniel Brown, Jochem Maas

This RFC will discuss an language enhancement for simple and easy array definition.

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 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

Discussion on the List

rfc/shortsyntaxforarrays.1212157654.txt.gz · Last modified: 2017/09/22 13:28 (external edit)