[wp-trac] [WordPress Trac] #53635: PHP 8.1: various compatibility fixes

WordPress Trac noreply at wordpress.org
Wed Jul 21 11:55:44 UTC 2021


#53635: PHP 8.1: various compatibility fixes
----------------------------+-------------------------------
 Reporter:  SergeyBiryukov  |       Owner:  (none)
     Type:  task (blessed)  |      Status:  new
 Priority:  normal          |   Milestone:  5.9
Component:  General         |     Version:
 Severity:  normal          |  Resolution:
 Keywords:  php81           |     Focuses:  coding-standards
----------------------------+-------------------------------

Comment (by jrf):

 Some context for the patch I just uploaded:

 This relates to the [https://wiki.php.net/rfc/internal_method_return_types
 Return types for internal methods RFC] in PHP 8.1 and in particular, the
 change made in [https://github.com/php/php-src/pull/7051 PHP PR #7051],
 which adds a `mixed` return type to the
 `JsonSerializable::jsonSerialize()` interface method.

 WP only contains one (test) class which implements the `JsonSerializable`
 interface and this patch fixes the issue for that class.

 Basically, as of PHP 8.1, the `jsonSerialize()` method in classes which
 implement the `JsonSerializable` interface are expected to have a return
 type declared. The return type should be `mixed` or a more specific type.
 This complies with the Liskov principle of covariance, which allows the
 return type of a child overloaded method to be more specific than that of
 the parent.

 The problem with this is that:
 1. The `mixed` return type was only introduced in PHP 8.0 and therefore
 cannot be used as WP has a minimum PHP version of 5.6.
 2. Return types in general were only introduced in PHP 7.0 and therefore
 cannot be used as WP has a minimum PHP version of 5.6.

 Luckily an attribute has been added to silence the deprecation warning.

 While attributes are a PHP 8.0+ feature, due to the choice of the `#[]`
 syntax, in PHP < 8.0, attributes will just be ignored and treated as
 comments, so there is no drawback to using the attribute.

 So to fix this, there is basically a choice between two options:
 * Either decouple from the interface. I'd strongly recommend against this
 as the interface is being implemented for a reason.
 * Or use the attribute, which is what this patch does.

 For more details, see the discussion in the upstream PR:
 https://github.com/php/php-src/pull/7051

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


More information about the wp-trac mailing list