[wp-trac] [WordPress Trac] #56514: PHP 8.2: fix magic method use within the test suite
WordPress Trac
noreply at wordpress.org
Wed Sep 7 15:59:31 UTC 2022
#56514: PHP 8.2: fix magic method use within the test suite
--------------------------------------------+-----------------------------
Reporter: jrf | Owner: SergeyBiryukov
Type: defect (bug) | Status: accepted
Priority: normal | Milestone: 6.1
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: has-patch php82 has-unit-tests | Focuses:
--------------------------------------------+-----------------------------
Comment (by SergeyBiryukov):
In [changeset:"54095" 54095]:
{{{
#!CommitTicketReference repository="" revision="54095"
Tests: Correct magic methods in `Basic_Object`.
This is a test fixture (dummy class only used in a test context), which
incorrectly implements the magic methods.
With the deprecation of dynamic properties in PHP 8.2, this needs to be
fixed.
The new implementation represents a “proper” implementation of the magic
methods for a class without non-`public` or typed properties.
Notes:
* Instead of relying on dynamic properties, the magic methods now store
properties in a `private` `$arbitrary_props` array and retrieve them from
there as well.
* The original `$foo` property, even though declared as `private`, was
never `private` in practice due to the way the magic methods were
originally implemented. In effect, it was fully publicly retrievable and
modifiable without any (type) restrictions. With that in mind, the `foo`
property has been moved into the `$arbitrary_props` array to keep the
implementation of the magic methods as clean and straightforward as
possible. With the adjusted magic methods, access to and modification of
`$foo` will (on the surface) continue to work in the same way as before,
while under the hood, it is no longer affected by the dynamic properties
deprecation.
* Take note of the use of `array_key_exists()` instead of `isset()` in the
`__get()` method. This is intentional and allows for `null` values to be
stored and retrieved.
* Also take note of `__set()` method no longer returning. `__set()` is
supposed to be a `void` method. In practice, the return value would always
be ignored due to how PHP handles magic methods, so in effect, this change
will not make any difference and does not constitute a backward
compatibility break.[[BR]][[BR]]
> The return value of `__set()` is ignored because of the way PHP
processes the assignment operator.
Alternatives considered:
* Instead of fixing the magic methods, they could have been removed
instead and the class be made to `extend` `stdClass`. It has been chosen
not to do so for two reasons:
1. It’s kind of nice to have at least ''one'' correct implementation of
magic methods in WP, which can be used as an example to point to as well.
2. Extending `stdClass` would change the class hierarchy, which ''may''
or ''may not'' affect the tests using this fixture (depending on what’s
being done with the class). Extending `stdClass` would also obfuscate
what’s going on in the class and would require extensive documentation to
prevent the extension being inadvertently removed at a future point in
time.
* Instead of fixing the magic methods, the test fixture could have been
deprecated and/or removed, with the few tests which use the fixture being
updated to use `stdClass` for their test fixture instead. It has been
chosen not to do so as there may well be external (plugin/theme) tests
relying on this test fixture and evaluating whether that is so would be
hard, as WP Directory cannot be used, since test code is normally not
included in the code published on wp.org. Also note, there is still a
(deprecated) `Basic_Subclass` fixture in the test suite, which extends
this class.
These magic methods and the `Basic_Object` test fixture were originally
introduced in [28480] and [28523]. The fixture was deprecated in [42381]
and undeprecated again in [45807].
At this time, the test fixture is used in the
`WP_Test_REST_Post_Meta_Fields` and the `Tests_REST_API` test classes.
References:
* [https://www.php.net/manual/en/language.oop5.overloading.php#object.set
PHP Manual: Overloading: __set()]
* [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC:
Deprecate dynamic properties]
* [https://github.com/php/php-src/issues/7786 php-src: #7786 PHP 8.2:
unexpected deprecation for dynamic property set via magic method]
Follow-up to [28480], [28493], [28523], [42381], [45807].
Props jrf, costdev.
See #56514.
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56514#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list