[wp-trac] [WordPress Trac] #56033: PHP 8.2: explicitly declare all known properties

WordPress Trac noreply at wordpress.org
Wed Jun 22 04:19:32 UTC 2022


#56033: PHP 8.2: explicitly declare all known properties
----------------------------+-----------------------------
 Reporter:  jrf             |      Owner:  (none)
     Type:  task (blessed)  |     Status:  new
 Priority:  normal          |  Milestone:  6.1
Component:  General         |    Version:
 Severity:  normal          |   Keywords:  has-patch php82
  Focuses:                  |
----------------------------+-----------------------------
 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

 **This ticket is intended ONLY for category 1 and 2 (see below), where
 fixing a typo or explicitly declaring the property fixes things.**

 == The problem

 === What is a dynamic property ?

 {{{#!php
 <?php
 class Foo {
     public $id;

     public function __construct( $id, $field_name ) {
         // This is fine, the $id property exists on the class.
         $this->id         = $id;

         // This is a dynamically created property as the property
         // is not declared on the class.
         $this->field_name = $field_name;
     }
 }
 }}}


 Dynamic properties can both be created from inside a class, like in the
 above example, as well as from outside the class (see the below example),
 which makes these issues very tricky to find via static analysis.

 {{{#!php
 <?php
 $obj = new Foo();
 $obj->type = 'something';
 }}}

 === When is something not a dynamic property ?

 1. When the property is explicitly declared on the class.
 2. When the property is explicitly declared on the (grand)parent class.


 === When are dynamic properties not problematic ?

 1. When the class, or one of its parents, has a magic `__set()` method
 which assigns the property to a declared property.
 {{{#!php
 <?php
 class Foo {
     private $fields = [];

     public function __construct($field_name ) {
         $this->field_name = $field_name; // Not problematic.
     }

     public function __set( $name, $value ) {
         $this->fields[ $name ] = $value;
     }
 }
 }}}
 2. When the class, or its parent, extends the PHP native `stdClass`, which
 has highly optimized versions of the `__set()` magic method available.

 == Mitigation

 The solution needed depends on the specific situation and each instance
 where a deprecation is thrown needs to be individually evaluated.

 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 at some point in the future.

 == Patches

 **Reminder: This ticket is intended ONLY for patches which fall in
 category 1 and 2, where fixing a typo or explicitly declaring the property
 fixes things.**

 A full set of patches for all such ''known'' situations - based on running
 the WP test suite against PHP 8.2 - is available and PRs for each will be
 opened on GitHub with a link to this ticket.

 However, as the WP test suite does not cover the full code base, more
 instances of situations which fall under category 1 and 2 are expected to
 be discovered over time.

 Patches for any additional instances found, which fall under category 1 or
 2, can be uploaded to this ticket and this ticket should stay open for
 these kind of patches during the WordPress 6.1 release cycle.


 ----

 Related: #56009


 Trac ticket # is open for addressing dynamic properties in category 3 and
 4.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56033>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list