Re: RFC: Short syntax for Arrays (redux)
| From: | Chris Stockton | Date: | Wed, 01 Jun 2011 21:29:00 +0000 |
| Subject: | Re: RFC: Short syntax for Arrays (redux) | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-52707@lists.php.net to get a copy of this message | ||
Hello,
On Wed, Jun 1, 2011 at 2:17 PM, Anthony Ferrara <ircmaxell@gmail.com> wrote:
> Personally, I think focusing on a character savings is the wrong
> reason to take on any problem. I don't care how long it takes for me
> to type code. I don't care how much extra hdd space it takes. But
> what I do care about is how readable it is.
>
> To me, the array shortcut syntax is pointless. Do you really mean to
> tell me that
[1, 2] is more readable/easier to understand than
> array(1,2)? To me, it's about a wash.
>
> However, the object shortcut syntax is a major win. For example:
>
> $person = new stdClass();
> $person->city = 'ogden';
> $person->state = 'ut';
> $perspn->country = 'usa';
> $person->favoriteNumbers = array(4, 12, 37, 42);
> $person->somethingWithNumbers();
> $person->unluckyNumbers = array(6, 13, 21);
> $person->likesPhp = 'very much so';
>
> vs
>
> $person = { 'name' => 'Justin',
> 'city' => 'ogden',
> 'state' => 'ut',
> 'country' => 'usa',
> 'favoriteNumbers' => [ 4, 12, 37, 42],
> 'unluckyNumbers' => [ 6, 13, 21],
> 'likesPhp' => 'very much so'
> }
>
> Did you notice what happened there? There's two differences. Try to
> find them. Neither would be possible with the shortcut syntax.
>
I don't think doing this is a very big deal, even in the case of Array().
$person = (object) [ 'name' => 'Justin',
'city' => 'ogden',
'state' => 'ut',
'country' => 'usa',
'favoriteNumbers' => [ 4, 12, 37, 42],
'unluckyNumbers' => [ 6, 13, 21],
'likesPhp' => 'very much so'
]