php deprecated array and string offset access syntax with curly braces is deprecated
PHP RFC: Deprecate curly brace syntax for accessing array elements and string offsets
Introduction
PHP allows both square brackets and curly braces to be used interchangeably for accessing array elements and string offsets. For example:
However, supporting both of these syntaxes can be confusing. Are there circumstances where one syntax behaves differently than the other? Is there a difference in performance between them? Is there some difference in scoping, since curly braces are the standard way to separate scope? What’s the purpose of the curly brace syntax?
Apart from two short notes in the PHP Manual, the curly brace syntax is virtually undocumented. Furthermore, it has reduced functionality compared to the normal bracket syntax. For example, it cannot be used for pushing an element into an array:
Nor can it be used to create an array:
It can’t be used for list assignment, either:
Proposal
Deprecate curly brace syntax for accessing array elements and string offsets.
Discussion
Wasn’t the curly brace syntax deprecated once before?
Is the curly brace syntax valuable for differentiating string and array offset access?
To make sure that code is indexing a string and not an array, a type check should be used instead of relying on syntax that can be used for both strings and arrays (and thus doesn’t tell you anything about the underlying type).
How frequently is the curly brace syntax used?
Nikita Popov checked the top 2k Composer packages, and found
2.2k individual uses of the curly brace array syntax. Compared to the 888.3k total array accesses in the data set, usage of the alternative syntax is about 0.25%. However, even this number is inflated somewhat due to duplicate packages (for example, there are two packages that mirror the WordPress Core repository, each with 182 usages). 92% of usages in the top 2k packages are in just 25 unique projects.
Will it be too much work for people to migrate code away from the curly brace syntax?
A migration script has been implemented alongside the deprecation patch: https://gist.github.com/theodorejb/763b83a43522b0fc1755a537663b1863
Backward Incompatible Changes
A deprecation warning will be output when using the curly brace syntax to access array or string offsets.
Started 3 July 2019. Ends 17th July 2019
Future Scope
Remove the feature entirely (replacing the deprecation warning with a compiler error) in PHP 8 or another future release.
PHP 7.4: Array and string offset access syntax with curly braces is deprecated #4263
Comments
BWPanda commented Jan 8, 2020 •
I get the following warning when running Backdrop on PHP 7.4:
Deprecated: Array and string offset access syntax with curly braces is deprecated in /
/core/modules/views/handlers/views_handler_filter_string.inc on line 275
I’m pretty sure we don’t officially support PHP v7.4 yet, but considering the fix is to use a syntax that works in older versions too then I don’t see why we can’t fix this easily now.
The text was updated successfully, but these errors were encountered:
We are unable to convert the task to an issue at this time. Please try again.
The issue was successfully created but we are unable to update the comment at this time.
indigoxela commented Jan 8, 2020
considering the fix is to use a syntax that works in older versions too
That’s not an easy fix.
PHP 5.3 doesn’t support [] array syntax at all, we can’t support both, 5.3 and 7.4.
BWPanda commented Jan 8, 2020
https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing doesn’t mention anything about certain PHP versions not supporting the square bracket syntax (unless I’m missing something).
indigoxela commented Jan 8, 2020
@BWPanda you’re right! I didn’t even know that this is supported currently. 😉 Never saw this anywhere before.
BWPanda commented Jan 8, 2020
indigoxela commented Jan 8, 2020
I found even two more usages of that syntax:
BWPanda commented Jan 8, 2020 •
I’m not sure if there are any other places in core where curly brackets are used in this way. This was just one I noticed when running a sandbox site on PHP v7.4. When I saw the warning and realised what caused it, I changed my sandbox to run on PHP v7.2 instead (no don’t have it to test anymore).
EDIT: @indigoxela Ah, well done! Welcome to update the PR if you like (I’m working on something else ATM but just wanted to make sure this was filed).
indigoxela commented Jan 8, 2020
I’m working on something else ATM
Me too, unfortunately.
BWPanda commented Jan 8, 2020
No worries, since I still had the repo open I added those you found to the PR.
indigoxela commented Jan 9, 2020 •
When trying to test your PR I realized some more PHP7.4 issues:
Do you prefer to handle it in this current issue or should this be a follow-up issue?
On which pages do you get the Deprecation warning? Can’t seem to find it anywhere in the logs, but that’s so crowded with the Notice.
I installed php7.4 just now on a Debian VM (sury repo, 7.4.1-1+0
20191218). And I tested with B 1.15.x-dev. But maybe I’m missing something.
UPDATE: I had a patch already applied to that crashburn testing web. But the Notice is still the same on a plain fresh 1.15.x, just on a different line in common.inc.
BWPanda commented Jan 9, 2020
Do you prefer to handle it in this current issue or should this be a follow-up issue?
If it’s a different error/warning/notice then probably a separate issue. This issue can be easily fixed and is backwards-compatible, other PHP v7.4 issues might not be.
I think I saw the notice at the top of the User list page/view ( /admin/people ).
indigoxela commented Jan 9, 2020
I think I saw the notice at the top of the User list page/view (/admin/people).
Still no luck. (I checked «Deprecated» on /admin/config/development/logging).
All I get is tons of the Notice re common.inc I mentioned.
Possibly Debian/sury patched out that deprecation in their php?
indigoxela commented Jan 9, 2020
Funny. by chance I came across a possible explanation: when I restart the corresponding php-fpm process (7.4 in this case), the Deprecation warning gets logged once in the error_log.
It never appears in the dblog.
So unfortunately I’m not able to test this PR. I could do a code review, though.
klonos commented Jan 9, 2020
FWIW, a long time ago, I have read someplace that PHP treats strings like arrays, so I guess I’ve always been using that.
Great catch @BWPanda, and thanks @indigoxela for picking up those other 2 places 👍
If it’s a different error/warning/notice then probably a separate issue. This issue can be easily fixed and is backwards-compatible, other PHP v7.4 issues might not be.
Array and string offset access syntax with curly braces is deprecated #5879
Comments
slps970093 commented Dec 18, 2019 •
Array and string offset access syntax with curly braces is deprecated
The text was updated successfully, but these errors were encountered:
We are unable to convert the task to an issue at this time. Please try again.
The issue was successfully created but we are unable to update the comment at this time.
GLedoussal commented Jan 6, 2020 •
TalonTR commented Jan 22, 2020
TalonTR commented Jan 22, 2020
TalonTR commented Jan 28, 2020 •
Nevertheless thanks for fixing this issue by removing the deprecated notation 👍
narfbg commented Jan 28, 2020 •
For one, we’d never name an array _compile_ ; then there’s the matching _compile_*() method names and last but not least, it’s practically documented.
As a side note, if I was wrong, then while the code would technically still work, simply making a notice go away is not a fix if the code doesn’t do what it was intended to. 🙂
Props for being vigilant though.
TalonTR commented Jan 28, 2020
For one, we’d never name an array compile
then there’s the matching compile*() method names
Yep, exactly, this confused me, too. PHP would allow for identical named properties & methods in parallel.
narfbg commented Jan 28, 2020
leo-rojas commented Aug 12, 2020
@narfbg hey, I know this is a few months old but do you know when the fix for this will be added to a release? Seeing in 3.1.11 the issue is still there.
Steps to reproduce the issue
Expected result
Actual result
PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/username/public_html/libraries/vendor/leafo/lessphp/lessc.inc.php on line 749
PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/username/public_html/libraries/vendor/leafo/lessphp/lessc.inc.php on line 1860
PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/username/public_html/libraries/vendor/leafo/lessphp/lessc.inc.php on line 2519
PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/username/public_html/libraries/vendor/leafo/lessphp/lessc.inc.php on line 2573
System information (as much as possible)
PHP 7.4.5
Joomla 3.9.18
Votes
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-05-01 19:29:10 |
Closed_By | ⇒ | sshcli |
Thanks @brianteeman for point me into the right direction.
Thanks @810 for give me the solution.
I’m closing this issue now
I have just update to 3.9.18 and get the following (PHP 7.4.3):
WordPress.org
Support » Theme: evolve » PHP Deprecated: Array and string offset access syntax with curly braces…
PHP Deprecated: Array and string offset access syntax with curly braces…
There is deprecation notice in my PHP error log file:
[22-Jan-2020 15:35:08 UTC] PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /path/to/wp-content/themes/evolve/inc/support.php on line 170
A version upgrade will be very much appreciated!
Thanks and regards,
joern
Until this is fixed in the theme’s source, anyone bothered by the deprecation notice (like I am 😉 ) can fix it by replacing the curly braces <> with square brackets [] in evolve/inc/support.php at line 170:
should be changed to
Hi,
I have a “similar” problem:
designer/includes/tcpdf/tcpdf.php on line 17008 Warning: “continue” targeting switch is equivalent to “break”. Did you mean to use “continue 2”? in /customers/f/8/d/ejmart.dk/httpd.www/wp-content/plugins/woocommerce-products-designer/includes/tcpdf/tcpdf.php on line 17864 Deprecated: Array and string offset access syntax with curly braces is deprecated in /customers/f/8/d/ejmart.dk/httpd.www/wp-content/plugins/woocommerce-products-designer/includes/tcpdf/include/tcpdf_images.php on line 310
and earlier I got a message saying on line 17005: plugins/woocommerce-products-designer/includes/tcpdf/tcpdf.php on line 17005 Deprecated: Array and string offset access syntax with curly braces is deprecated in /customers/f/8/d/ejmart.dk/httpd.www/wp-content/plugins/woocommerce-products-designer/includes/tcpdf/tcpdf.php on line 17008 Warning: “continue” targeting switch is equivalent to “break”. Did you mean to use “continue 2”?
Could this also be solved by changing braces and if so, where do I find the document to make the changes? I am not familiar with this, so will highly appreciate guidance on not just what to do but also where and how? Hope so much you can help. 😃🙏