#58897 closed defect (bug) (fixed)
Fix WP_User_Query magic methods for PHP 8.2 dynamic property
Reported by: | antonvlasenko | Owned by: | hellofromTonya |
---|---|---|---|
Milestone: | 6.4 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Users | Keywords: | php82 has-patch has-unit-tests commit |
Focuses: | php-compatibility | Cc: |
Description (last modified by )
WP_User_Query
has a list of compatible fields (compat_fields
), which was introduced at the same time as the magic methods. The fields in compat_fields
list are then not dynamic properties, as the magic methods properly handle each. But fields not in that list are dynamic properties.
What happens if a field is not in the list?
__get()
falls through the method.__set()
nothing happens as it falls through the method.__isset()
returnsfalse
, which is correct.__unset()
nothing happens as it falls through the method.
The problem:
When a dynamic property is called on this class, there's no information given to the developer. The notice of _doing_it_wrong()
will provide the alert to developers to modify their code.
Also notice: the compat_fields
are essentially public properties through the magic methods.
Proposed Solutions
As proposed in #58896, 2 ways to fix this class to be compatible with PHP 8.2's dynamic properties:
- Option 1: Fix the magic methods:
- Change 1: Add a notice or
_doing_it_wrong()
to each magic method, i.e. to alert and inform developers when attempting to get/set/isset/unset a dynamic property.
- Change 2: Add
return null
in__get()
when attempting to get a dynamic property that is not in thecompat_fields
.
- Option 2: Add each
compat_fields
as apublic
property and remove the magic methods.
Props to @jrf for identifying the issue.
References:
- These changes were discussed and agreed to in a livestream by @jrf @markjaquith and @hellofromTonya.
- The changes are part of #56034, which is the larger Dynamic Properties proposal and initiative.
Change History (15)
This ticket was mentioned in PR #4903 on WordPress/wordpress-develop by @rajinsharwar.
17 months ago
#2
- Keywords has-patch added; needs-patch removed
#3
@
17 months ago
Tried to follow with Option 1; added a notice or _doing_it_wrong() to each magic method, added the return null in get() when attempting to get a dynamic property that is not in the compat_fields.
This ticket was mentioned in PR #4906 on WordPress/wordpress-develop by @antonvlasenko.
17 months ago
#4
- Keywords has-unit-tests added; needs-unit-tests removed
This PR aims to call doint_it_wrong()
when trying to use dynamic properties on the WP_List_Table
class. Dynamic properties are not compatible with PHP 8.2 and above.
Trac ticket: https://core.trac.wordpress.org/ticket/58897
#5
@
16 months ago
Historical context:
Original design before the magic methods
- Added the
WP_User_Query
class. - Marked its
var $results
andvar $total_users
as@access private
, meaning intended for only internal usage within the object.
Changes that added the magic methods
These changes were made for backwards-compatibility (BC), as the properties were public (via var
) and thus might have been used outside of WordPress Core.
During 4.0.0, [28528] / #27881:
- Added the
_get()
,__set()
,__isset()
and__unset()
magic methods - Added visibility keywords for properties and methods, including replacing
var
withprivate
for the 2 properties.
During 4.2.0, [31144] / #30891:
- Added the list of
compat_fields
and the logic in each of the property magic methods.
#6
@
16 months ago
- Keywords changes-requested added
The history and design intent in WP_User_Query
properties and magic methods is the same as WP_List_Table
, see #58896. After extensive and careful review and consideration, Option 1 with deprecations was selected and committed [56349].
I think the same approach should be used in WP_User_Query
.
To expedite, let's combine the work into https://github.com/WordPress/wordpress-develop/pull/4906, which @antonvlasenko is in the process of updating for the changes made in [56349].
@hellofromTonya commented on PR #4903:
16 months ago
#7
Hello @Rajinsharwar 👋 Thank you for contributing by creating this PR.
Instead of using _doing_it_wrong()
, let's use the same strategy done in WP_List_Table
changeset https://core.trac.wordpress.org/changeset/56349 that deprecates the magic methods for dynamic properties and fixes a couple of issues in the magic methods. It also adds happy and unhappy tests for each of the magic methods.
I hope you don't mind, but let's consolidate this work into 1 PR and collaborate together. I'll close this in favor of https://github.com/WordPress/wordpress-develop/pull/4906.
@hellofromTonya commented on PR #4906:
16 months ago
#8
As we discussed, @anton-vlasenko please update this PR to use the same code strategy done in https://core.trac.wordpress.org/changeset/56349.
@antonvlasenko commented on PR #4906:
16 months ago
#9
@hellofromtonya Could you please review this PR?
I've updated it.
e2e test are failing, but it seems to be unrelated to the changes in this PR.
#10
@
16 months ago
- Keywords commit added; changes-requested removed
- Status changed from assigned to reviewing
PR 4906 is ready for commit
.
Self-assigning for commit.
@hellofromTonya commented on PR #4906:
16 months ago
#13
Committed via https://core.trac.wordpress.org/changeset/56353
Fixing the magic methods of the WP_User_Query class
Trac ticket: https://core.trac.wordpress.org/ticket/58897