# Proposal for deprecate alternate syntax(curly braces) for array and string access

- Thread: https://externals.io/message/104744
- Original archive: https://news-web.php.net/php.internals/104744
- Started by Andrey O Gromov on 2019-03-15T13:39:13+00:00
- Messages: 8
- RFC: Deprecate alternate access to array elements and chars in string (Implemented (PHP 7.4)) — https://externals.io/rfc/deprecate_curly_braces_array_access.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #104744 — Andrey O Gromov — 2019-03-15T13:39:13+00:00

URL: https://externals.io/message/104744#104744

Reasons to deprecate curly braces syntax::
1.      Two ways to do the same thing.
2.      It is very rarely used nowadays.
3.      It is almost not documented. There is only two short “NOTE” about 
it.
4.      Also, this syntax has reduced functionality. You can't use it for 
pushing element into array “$arr{} = 1;”, creating array “$a={1,2};” or in 
other similar cases.
5.      Deprecation and following removal will free this syntax for other 
features. For example: array/string slice, absolute offset access, 
“windows” (like “slice” in GoLang), etc.

Proposal
https://wiki.php.net/rfc/deprecate_curly_braces_array_access

Patch diff
https://github.com/php/php-src/compare/PHP-7.4...rjhdby:deprecate_alternate_array_access

Migration script
https://github.com/rjhdby/php-src/blob/deprecate_alternate_array_access/convert_array_access_braces.php

---

## #104745 — Michael Wallner — 2019-03-15T13:43:38+00:00

URL: https://externals.io/message/104744#104745

> Reasons to deprecate curly braces syntax::
> 1.      Two ways to do the same thing.
> 2.      It is very rarely used nowadays.
> 3.      It is almost not documented. There is only two short “NOTE” about 
> it.
> 4.      Also, this syntax has reduced functionality. You can't use it for 
> pushing element into array “$arr{} = 1;”, creating array “$a={1,2};” or in 
> other similar cases.
> 5.      Deprecation and following removal will free this syntax for other 
> features. For example: array/string slice, absolute offset access, 
> “windows” (like “slice” in GoLang), etc.
> 
> Proposal
> https://wiki.php.net/rfc/deprecate_curly_braces_array_access


Do I understand it right, that you're proposing deprecating accessing
$string{$offset}? I think that's an important way of differentiation
between string and array offset access.

I'd vote "yes" for splitting syntax on array and string offset access,
but as is: -1.

> 
> Patch diff
> https://github.com/php/php-src/compare/PHP-7.4...rjhdby:deprecate_alternate_array_access
> 
> Migration script
> https://github.com/rjhdby/php-src/blob/deprecate_alternate_array_access/convert_array_access_braces.php
> 


-- 
Regards,
Mike

---

## #104746 — Nikita Popov — 2019-03-15T15:05:48+00:00

URL: https://externals.io/message/104744#104746

> Reasons to deprecate curly braces syntax::
> 1.      Two ways to do the same thing.
> 2.      It is very rarely used nowadays.
> 3.      It is almost not documented. There is only two short “NOTE” about
> it.
> 4.      Also, this syntax has reduced functionality. You can't use it for
> pushing element into array “$arr{} = 1;”, creating array “$a={1,2};” or in
> other similar cases.
> 5.      Deprecation and following removal will free this syntax for other
> features. For example: array/string slice, absolute offset access,
> “windows” (like “slice” in GoLang), etc.
>
> Proposal
> https://wiki.php.net/rfc/deprecate_curly_braces_array_access
>
> Patch diff
>
> https://github.com/php/php-src/compare/PHP-7.4...rjhdby:deprecate_alternate_array_access
>
> Migration script
>
> https://github.com/rjhdby/php-src/blob/deprecate_alternate_array_access/convert_array_access_braces.php


I'm okay with this. This syntax has already been deprecated once, though it
was reverted for reasons I don't remember.

There are some people using this syntax to distinguish between array and
string access. It's a nice thought, but as the vast majority of code
doesn't make this distinction (I think the only time I saw this in recent
years was inside some old PEAR code), it's not really a useful indicator.
The rarity of its use also makes it rather confusing. While $foo[$x] is
well established as an array or string offset (or for that matter,
ArrayObject) access and will be recognized by any programmer coming from
any number of programming languages, $foo{$x} certainly is not, and is a
WTF moment for people who don't happen to personally use this syntax.

I'd prefer to phase out this syntax entirely and not reuse it for any other
purpose. Reusing syntax is generally a not so good idea, because it means
that the same syntax has different meaning in different PHP version.

Nikita

---

## #104755 — Kalle Sommer Nielsen — 2019-03-15T16:31:25+00:00

URL: https://externals.io/message/104744#104755

Den fre. 15. mar. 2019 kl. 17.06 skrev Nikita Popov <nikita.ppv@gmail.com>:
> I'm okay with this. This syntax has already been deprecated once, though it
> was reverted for reasons I don't remember.
>
> There are some people using this syntax to distinguish between array and
> string access. It's a nice thought, but as the vast majority of code
> doesn't make this distinction (I think the only time I saw this in recent
> years was inside some old PEAR code), it's not really a useful indicator.

I personally use it because I like to quickly tell if I am doing an
operation on a string or array, it is eye candy and makes a lot of
sense. I think if anything the two syntaxes should be decoupled
instead.

From a usage PoV, then from personal experience (outside my own
projects), I have seen many usages of this syntax, even as late as my
current company with a growing codebase that is nearing 1.1m LoC, I
have seen this syntax used countless of times. In fact I don't think I
have been working with a code base where I have not seen this syntax
used.

I have asked fellow developers around in my community today and they
have no strong opinion on either, but do like the distinction between.

> The rarity of its use also makes it rather confusing. While $foo[$x] is
> well established as an array or string offset (or for that matter,
> ArrayObject) access and will be recognized by any programmer coming from
> any number of programming languages, $foo{$x} certainly is not, and is a
> WTF moment for people who don't happen to personally use this syntax.
>
> I'd prefer to phase out this syntax entirely and not reuse it for any other
> purpose. Reusing syntax is generally a not so good idea, because it means
> that the same syntax has different meaning in different PHP version.


For me personally it is a huge -1, if anything I would like the
string[] syntax decoupled.


-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #104758 — Levi Morrison — 2019-03-15T17:52:39+00:00

URL: https://externals.io/message/104744#104758

>
> Den fre. 15. mar. 2019 kl. 17.06 skrev Nikita Popov <nikita.ppv@gmail.com>:
> > […]
>
> I personally use it because I like to quickly tell if I am doing an
> operation on a string or array, it is eye candy and makes a lot of
> sense. I think if anything the two syntaxes should be decoupled
> instead.
>
> From a usage PoV, then from personal experience (outside my own
> projects), I have seen many usages of this syntax, even as late as my
> current company with a growing codebase that is nearing 1.1m LoC, I
> have seen this syntax used countless of times. In fact I don't think I
> have been working with a code base where I have not seen this syntax
> used.
>
> I have asked fellow developers around in my community today and they
> have no strong opinion on either, but do like the distinction between.
>
> > […]
>
>
> For me personally it is a huge -1, if anything I would like the
> string[] syntax decoupled.
>
>
> --
> regards,
>
> Kalle Sommer Nielsen
> kalle@php.net
>
> --
> 
> 

Four thoughts:

  1. I cannot think of *any* reason to separate them. If you want to
make sure you are indexing a string, *do a type check*, not bake `{}`
into only working on strings.
  2. While Kalle says pretty much every codebase they've seen lately
has used `{}` for indexing, I have *never* seen a codebase that used
it.
  3. In any case, usage of `{}`  can be migrated to `[]` by a style
fixer, right?
  4. Even if we deprecate `{}`, I don't think we'd be in any hurry to
remove it, though with an automatic fixer this seems doable, if we
care to do it at all.

---

## #104759 — Rowan Collins — 2019-03-15T18:08:22+00:00

URL: https://externals.io/message/104744#104759

>   1. I cannot think of *any* reason to separate them. If you want to
> make sure you are indexing a string, *do a type check*, not bake `{}`
> into only working on strings.
>


I think it's more common to do the opposite: think you're working on an
array, and accidentally pick up a string.

If we separated the syntaxes, it would be possible to always raise a
warning when that happens, rather than silently returning the Nth character.

To be honest I can't remember the last time I used *either* syntax for
string access, other than by accident.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #104760 — Kalle Sommer Nielsen — 2019-03-15T18:23:32+00:00

URL: https://externals.io/message/104744#104760

Den fre. 15. mar. 2019 kl. 19.53 skrev Levi Morrison <levim@php.net>:
> Four thoughts:
>
>   1. I cannot think of *any* reason to separate them. If you want to
> make sure you are indexing a string, *do a type check*, not bake `{}`
> into only working on strings.
>   2. While Kalle says pretty much every codebase they've seen lately
> has used `{}` for indexing, I have *never* seen a codebase that used
> it.

Well maybe that happens to be just me, not a huge amount of usages but
there is usually some.

>   3. In any case, usage of `{}`  can be migrated to `[]` by a style
> fixer, right?
>   4. Even if we deprecate `{}`, I don't think we'd be in any hurry to
> remove it, though with an automatic fixer this seems doable, if we
> care to do it at all.

The thing is, I just don't understand the rationale to remove this
feature, what the full gain from doing so is?

-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #104783 — Andrey O Gromov — 2019-03-18T10:37:27+00:00

URL: https://externals.io/message/104744#104783

> > […]
and
> > […]
recent
> > […]
indicator.

> I personally use it because I like to quickly tell if I am doing an
> operation on a string or array, it is eye candy and makes a lot of
> sense. I think if anything the two syntaxes should be decoupled
> instead.

I think that suitable naming is more useful for this purpose. 
As this syntax used both for arrays and strings then "$a{2}" 
says absolutely nothing to anybody instead author, maybe.

> The thing is, I just don't understand the rationale to remove this
> feature, what the full gain from doing so is?

I'm fully agree with Nikita Popov:

> > […]
is well established as an 
> > […]
will be 
> > […]
languages, 
> > […]
happen to personally 
> > […]

Furthermore, curly braces is standard way to separate scope in almost 
all languages including PHP (except one very rare and special case).

@Michael Wallner
> Do I understand it right, that you're proposing deprecating accessing
> $string{$offset}? I think that's an important way of differentiation
> between string and array offset access.
>
> I'd vote "yes" for splitting syntax on array and string offset access,
> but as is: -1.

When we access char inside string, actually we access 
to element of indexed array of chars.

Splitting array and string access is complex change. 
For now it processed on AST-creation phase and it is possible 
to transfer to compiler via AST-attribute. For splitting 
it is needs to transfer this difference into runtime.

In addition, this separation can lead to much more ambiguity when 
migrating old code.
