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

php.internals

Andrey O Gromov

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

Michael Wallner

7 years ago
On 15/03/2019 14:39, Andrey O Gromov wrote:
> 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

Nikita Popov

7 years ago
On Fri, Mar 15, 2019 at 2:39 PM Andrey O Gromov <AGromov@alfabank.ru> wrote:
> 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

Kalle Sommer Nielsen

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

Levi Morrison

7 years ago
On Fri, Mar 15, 2019 at 10:31 AM Kalle Sommer Nielsen <kalle@php.net> wrote:
> > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
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.

Rowan Collins

7 years ago
On Fri, 15 Mar 2019 at 17:53, Levi Morrison <levim@php.net> wrote:
> 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]

Kalle Sommer Nielsen

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

Andrey O Gromov

7 years ago
> > 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.
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:
> > > 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.
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.