On Thu, 9 Jan 2020 at 10:48, Nikita Popov <nikita....@gmail.com> wrote:
> PHP has a couple of legacy string interpolation syntaxes, the most > egregious being "$array[foobar]". The issue with this syntax is that the > array key is not quoted, is required to be not quoted and is silently > accepted. > > ... > > What do you think about this? I think my personal preference would be > towards phasing out the syntax entirely: If we're going to make people > migrate, we can just as well migrate them towards our generic and preferred > interpolation syntax. > Hi Nikita, I agree that this syntax is peculiar, and think you're right that the most consistent way forward is to phase out both "$array[foobar]" and "$array[0]". The biggest problem I see with allowing "$array['foobar']" as an alternative spelling is that it makes the inconsistent handling of the bare string even more surprising. Consider these pairs: const DEBUG='yes' $array = [ 'DEBUG' => 'blah blah blah', 'yes' => 'yes yes yes' ]; // Normal PHP code echo $array['DEBUG']; // quoted string as key - echoes blah blah blah echo $array[DEBUG]; // constant as key - echoes yes yes yes // Properly delimited string interpolation echo "{$array['DEBUG']}"; // quoted string as key inside string interpolation - echoes blah blah blah echo "{$array[DEBUG]}"; // constant as key inside string interpolation - echoes yes yes yes // Shorthand string interpolation; only the second form currently compiles echo "$array['DEBUG']"; // quoted string as key inside string interpolation - echoes blah blah blah echo "$array[DEBUG]"; // looks like a constant, but acts like a string! - echoes blah blah blah My only hesitation is the usual one for language changes: how much working code will need to be amended, and how easy is it to identify and correctly fix that code? Since the current syntax isn't ambiguous, it ought in principle to be possible for a tool to parse source code and identify and fix instances. I think in cases like this, the upgrading notes should link specifically to one or more such tools. Even with such a tool, it's potentially quite a disruptive change, so we need to decide how important we think removing this syntax would be. Regards, -- Rowan Tommins [IMSoP]