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

WordPress Trac noreply at wordpress.org
Mon Aug 2 12:14:10 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:
----------------------------------------------+---------------------

Comment (by jrf):

 Some example code which could be used for the dev-note for how plugins can
 make their code WP cross-version compatible as well as PHP cross-version
 compatible:
 {{{#!php
 <?php
 // Option 1: polyfill the new function (preferred solution).
 if ( ! function_exists( 'wp_readyonly' ) ) {
     function wp_readonly( $readonly, $current = true, $echo = true ) {
         return __checked_selected_helper( $readonly, $current, $echo,
 'readonly' );
     }
 }

 // Option 2: conditionally call the function.
 if ( function_exists( 'wp_readyonly' ) ) {
     $code = wp_readonly( $readonly, $current, $echo );
 } elseif ( PHP_VERSION_ID < 80100 ) {
     // Using `call_user_func()` prevents this code from being a parse
 error.
     $code = call_user_func( 'readonly', $readonly, $current, $echo );
 } else {
     $code = __checked_selected_helper( $readonly, $current, $echo,
 'readonly' );
 }
 }}}

 These code samples should work for WP 2.8 (version in which
 `__checked_selected_helper()` was introduced) to current.

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


More information about the wp-trac mailing list