[wp-trac] [WordPress Trac] #53858: PHP 8.1: syntax error due to new 'readonly' property

WordPress Trac noreply at wordpress.org
Mon Aug 2 11:47:14 UTC 2021

#53858: PHP 8.1: syntax error due to new 'readonly' property
 Reporter:  haosun                            |       Owner:  (none)
     Type:  defect (bug)                      |      Status:  new
 Priority:  normal                            |   Milestone:  5.9
Component:  General                           |     Version:  4.9
 Severity:  normal                            |  Resolution:
 Keywords:  needs-patch php81 needs-dev-note  |     Focuses:
Changes (by jrf):

 * keywords:  needs-patch php81 => needs-patch php81 needs-dev-note


 > I know. I meant (by 'calls') that the `readonly()` alias just should
 call the new `wp_readonly()`, and only be defined in case PHP < 8.1.
 > Renaming to `wp_readonly()` and making `readonly()` an alias of that on
 PHP < 8.1 sounds reasonable as long as it doesn't cause syntax errors
 anymore. It should be marked as deprecated though.

 I've read through the discussion so far and would like to add some
 1. This function ''is'' used in - at least in userland code -, though
 luckily not very often. Here is a
 [https://wpdirectory.net/search/01FC39KK20BEK3V06X1PSBDQJV WP Directory
 plugin search] showing this. Lots of false positives for JS code, but also
 some real uses and even polyfill declarations for when WP didn't have the
 function yet. A [https://wpdirectory.net/search/01FC3A2BFQARC0EMYY2CAYGTBF
 Theme Search for the same] did not yield any valid results.
 2. While this will be even more difficult to search for, the function
 ''may'' also be used as a callback via `add_filter()`. To be honest, I
 wouldn't be surprised if that type of use exists in the WP Core codebase,
 though I haven't done a search for it (yet). In that case, in PHP 8.1, I
 expect this will result in a run-time fatal error when `readonly` gets
 called via `apply_filters()`. This type of use definitely also
 [https://wpdirectory.net/search/01FC3AQG97EF12JQ1M2H6RSWD1 exists in
 plugins], like can be seen in the "Booster for WooCommerce" plugin.
 3. A plain renaming of the function is a BC-break which is unacceptable in
 the WP landscape as it **will** break integrations.
 4. Aliasing the function in the manner @swissspidy suggest in `wp-
 includes/deprecated.php` will not work as `readonly` will now be full
 keyword in PHP, which means that loading the file with the original
 deprecated function would yield a parse error in PHP 8.1, i.e. result in a
 white screen of death.

 > Btw: I wonder if just enclosing the function in an if statement is
 enough. Maybe we have to include a file conditionally?

 So yes, to move forward, I'd like to suggest the following:
 1. Rename the function in place.
 2. Add a **new** file which only contains the old, deprecated `readonly`
 function and - this is the important part - **only load this file
 conditionally when PHP < 8.1 is detected**.

 Plugins/themes which use the function **and** want to support PHP 8.1 will
 need to switch to the new function name ASAP, as ''calls'' to the
 `readonly()` function in their code on PHP 8.1 will be a parse error just
 the same.

 This should get a prominent mention in the WP 5.9 dev-note about PHP 8.1.

Ticket URL: <https://core.trac.wordpress.org/ticket/53858#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list