Make WordPress Core

Opened 23 months ago

Closed 21 months ago

Last modified 21 months ago

#56513 closed task (blessed) (fixed)

PHP 8.2: silence dynamic properties deprecation notices

Reported by: jrf's profile jrf Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.1 Priority: normal
Severity: normal Version: 6.1
Component: General Keywords: has-patch php82 commit
Focuses: Cc:

Description

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0, though this last part is not 100% certain yet.

RFC: https://wiki.php.net/rfc/deprecate_dynamic_properties

We can basically categorize dynamic properties into four base situations:

Situation Solution
1. Typo in property name Fix the typo, either in the property as declared or where the property assignment happens
2. Known, named, dynamic property Declare the property on the (parent) class
3. Known use of unknown dynamic properties Declare the full set of magic methods on a class (preferred) or let the class extend stdClass (discouraged)
4. Unknown use of unknown dynamic properties Use the #[AllowDynamicProperties] attribute on the (parent) class and/or declare the full set of magic methods on a class and/or let the class extend stdClass

Note: the #[AllowDynamicProperties] attribute is expected to be a temporary solution and it is expected that support for the attribute will be removed from PHP at some point in the future.

Current status

Patches have been created for Trac #56033 to explicitly declare all "known" dynamic properties (type 1 + 2).

A proposal to mitigate the deprecation for the "unknown" dynamic properties (type 3 + 4) via traits is open in Trac #56034.

However, it is clear that more time is needed to action that proposal.

Interim solution

For the time being, I propose to add the #[AllowDynamicProperties] attribute on all parent classes in the src directory and classes which extend a class from an external dependency.

This should buy us more time to get a long-term solution in place via #56034.

The main reasons to add the attribute for now, is to silence "noise" in the form of:

  1. New (duplicate) Trac tickets being opened about the dynamic properties deprecation.
  2. WP Core tests error-ing out on the deprecation notices when run against PHP 8.2.
  3. Integration tests for plugins/themes error-ing out on the deprecation notices when run against PHP 8.2.
  4. Deprecation notices being logged in error logs on servers which have error logging for this turned on (not the default setup for WP).

As an additional advantage, this will allow us to remove the continue-on-error for PHP 8.2 test runs in the foreseeable future, which will help prevent new PHP 8.2 incompatibilities from being introduced.

There is already a PR open on GitHub for this ticket: https://github.com/WordPress/wordpress-develop/pull/3123

If/when new classes get added to WP in the mean time (before the solution from #56034 is in place), tooling has been created to either update the current patch and/or create additional patches using the same logic as used to create the current patch.

Attachments (3)

56513-update-for-new-classes.patch (4.5 KB) - added by jrf 22 months ago.
Patch to add teh attribute to new classes introduced in WP 6.1
56513-themes.patch (7.6 KB) - added by jrf 22 months ago.
Optional patch to add the new attribute to the WP native themes
56513-themes.2.patch (7.5 KB) - added by jrf 22 months ago.
Optional patch to add the new attribute to the WP native themes

Download all attachments as: .zip

Change History (19)

#1 @peterwilsoncc
23 months ago

#56511 was marked as a duplicate.

#2 @peterwilsoncc
23 months ago

There are two approvals on the related pull request (from @costdev and me) so this is commit ready.

#3 @aristath
23 months ago

As a temporary measure, this patch looks good to me 👍

#4 @knutsp
23 months ago

As suggested in https://core.trac.wordpress.org/ticket/56009#comment:1
Then tickets for a class or group of similar classes to remove the need for allowing dynamic properties, as there are time and resources to do it properly. It also seems the suggested way to start from the PHP community https://stitcher.io/blog/deprecated-dynamic-properties-in-php-82#a-better-alternative

#5 @SergeyBiryukov
22 months ago

In 54133:

Code Modernization: Add AllowDynamicProperties attribute to all (parent) classes.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

There are a number of ways to mitigate this:

  • If it is an accidental typo for a declared property: fix the typo.
  • For known properties: declare them on the class.
  • For unknown properties: add the magic __get(), __set(), et al. methods to the class or let the class extend stdClass which has highly optimized versions of these magic methods built in.
  • For unknown use of dynamic properties, the #[AllowDynamicProperties] attribute can be added to the class. The attribute will automatically be inherited by child classes.

Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.

To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the #[AllowDynamicProperties] attribute to all “parent” classes in WP.

The logic used for this commit is as follows:

  • If a class already has the attribute: no action needed.
  • If a class does not extend: add the attribute.
  • If a class does extend:
    • If it extends stdClass: no action needed (as stdClass supports dynamic properties).
    • If it extends a PHP native class: add the attribute.
    • If it extends a class from one of WP's external dependencies: add the attribute.
  • In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.

Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the live stream from August 16, 2022 for more details.

This commit only affects classes in the src directory of WordPress core.

  • Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
  • While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.

Reference: PHP RFC: Deprecate dynamic properties.

Follow-up to [53922].

Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.

This ticket was mentioned in Slack in #core by sergey. View the logs.


22 months ago

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


22 months ago

#8 @audrasjb
22 months ago

  • Type changed from defect (bug) to task (blessed)

Switching it to a blessed task, as it is actively worked on by experimented people :)

#9 @SergeyBiryukov
22 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

#10 @jrf
22 months ago

Just as a FYI: while the initial patch has been committed, this ticket remains open for the time being to:

  1. Discuss whether the principle of the patch should also be applied to WordPress native default themes.
  2. Allow for a follow-up patch for the same to be applied to any new classes being introduced in WP 6.1.

#11 @peterwilsoncc
22 months ago

  • Keywords commit removed

Removing the commit keyword now the initial patch has been committed to move this out of the commit section of the workflow report.

@jrf
22 months ago

Patch to add teh attribute to new classes introduced in WP 6.1

@jrf
22 months ago

Optional patch to add the new attribute to the WP native themes

#12 @jrf
22 months ago

  • Keywords commit added

I've added two patches to this ticket:

  1. With an update to the previous patch to add the attribute to class newly introduced in WP 6.1 (since the previous patch was committed).
  2. An optional patch to also add the attribute to classes from the WP native themes. I'm not sure we should do this, but if it would be decided, we should, at least a patch is available.

I'm adding the commit keyword to get the "update" patch committed before WP 6.1-RC.

@jrf
22 months ago

Optional patch to add the new attribute to the WP native themes

#13 @jrf
22 months ago

Uploaded a new version of the "themes" patch, now with tabs instead of spaces ;-)

#14 @SergeyBiryukov
21 months ago

In 54481:

Code Modernization: Add AllowDynamicProperties attribute to recently introduced classes.

This commit is a follow-up to [54133] for new classes introduced in WordPress 6.1 since the previous commit.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

There are a number of ways to mitigate this:

  • If it is an accidental typo for a declared property: fix the typo.
  • For known properties: declare them on the class.
  • For unknown properties: add the magic __get(), __set(), et al. methods to the class or let the class extend stdClass which has highly optimized versions of these magic methods built in.
  • For unknown use of dynamic properties, the #[AllowDynamicProperties] attribute can be added to the class. The attribute will automatically be inherited by child classes.

Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.

To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, the #[AllowDynamicProperties] attribute has been added to all “parent” classes in WP.

Reference: PHP RFC: Deprecate dynamic properties.

Follow-up to [53922], [54133].

Props jrf.
See #56513, #56034.

#15 @SergeyBiryukov
21 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

I believe this is done for 6.1. Let's continue in #56034 for 6.2.

#16 @jrf
21 months ago

Thanks @SergeyBiryukov !

Note: See TracTickets for help on using tickets.