PHP 8.2 - Fix deprecated dynamic properties

Created on 19 December 2022, about 2 years ago
Updated 14 November 2023, about 1 year ago

Problem/Motivation

Under PHP 8.2, there are a lot of messages like Creation of dynamic property ... is deprecated. For example, see these test results from Election .

Proposed resolution

Enabled PHP 8.2 testing in Views. Declare all the properties used or mark each class with #[\AllowDynamicProperties].

📌 Task
Status

Fixed

Version

1.0

Component

Code - misc

Created by

🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

Live updates comments and jobs are added and updated live.
  • PHP 8.2

    The issue particularly affects sites running on PHP version 8.2.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States tr Cascadia

    Well again, how much of this is because of core?

    Take for example is_new, which is a well-known and often-discussed dynamic property that is defined and used by core. In this case, Entity API is simply using core. Modifying Entity API to try to avoid PHP 8.2 errors resulting from how core uses is_new is just the wrong thing to do - we should not be fixing every single contrib module, we should be fixing core.

    Has is_new as a dynamic property been addressed in core yet? I don't see any indication at all that core's usage of is_new has changed. From your "error" output, it complains 432 times about is_new as a dynamic property - ALL those errors will go away when core is made compliant with PHP 8.2. NONE of those errors need to be or should be addressed in the Entity API.

    What needs to be done here is to identify which of the 10,000+ errors in your output are due to the Entity API, because those are the ONLY ones that should be addressed here. I would say that the vast majority of these are from core.

    After core works in 8.2, THEN we can see what errors show up when adding in Entity API, and THEN we can start to address them one-by-one. And that process will be greatly enhanced by people contributing patches targeted to fix specific dynamic property errors.

  • Status changed to Active almost 2 years ago
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    When I submitted this, I had mis-read which module was setting the dynamic property versus which one contained the class in question. It may be that entity doesn't have any classes that need to be changed.

    For now, I suggest enabling testing of entity on PHP 8.2 and the postponing if that raises no issues. There are other modules that pass testing on PHP 8.2.

  • 🇺🇸United States tr Cascadia

    Here is the testbot output for PHP 8.2: https://www.drupal.org/pift-ci-job/2579786

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    Thanks.

    There are a bunch of error messages like:

    Creation of dynamic property Entity::$data is deprecated

    Here are all the properties that are raising this error in class Entity:

    Entity::$data
    Entity::$id
    Entity::$is_new
    Entity::$is_rebuild
    Entity::$label
    Entity::$locked
    Entity::$module
    Entity::$name
    Entity::$original
    Entity::$rdf_mapping
    Entity::$status
    Entity::$uid
    Entity::$weight
    

    Declaring these properties would fix these errors in class Entity. It would also be fixed by adding #[\AllowDynamicProperties] to that class.

  • 🇸🇰Slovakia poker10

    Has is_new as a dynamic property been addressed in core yet? I don't see any indication at all that core's usage of is_new has changed. From your "error" output, it complains 432 times about is_new as a dynamic property - ALL those errors will go away when core is made compliant with PHP 8.2. NONE of those errors need to be or should be addressed in the Entity API.

    PHP 8.2 tests for core are passing. Therefore this is either not covered by tests, or it is not an issue in the core. I have not seen a report about PHP 8.2 deprecation of is_new in the D7 core issue queue. But I will try to verify this dynamic declaration on the D7 core side. Anyway, I agree that if something like this originates from the core, we should fix it there.

  • 🇸🇰Slovakia poker10

    As @Liam Morland pointed out, this module defines an Entity class. Such class cannot declare dynamic properties in PHP 8.2. Drupal core uses stdClass for entities (such as nodes) and it is allowed to declare dynamic properties on stdClass. Therefore this seems to be related only to the entity module.

    See: https://php.watch/versions/8.2/dynamic-properties-deprecated#exempt

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    47 pass
  • 🇸🇰Slovakia poker10

    I think that at least these dynamic properties needs to be taken care of. In the Entity I have used #[\AllowDynamicProperties], elsewhere declared the missing properties. Let's check the tests if there are any more issues.

  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    46 pass, 8 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    47 pass
  • 🇸🇰Slovakia poker10

    Missed one dynamic property EntityMetadataIntegrationTestCase::$node (see: https://www.drupal.org/pift-ci-job/2655384 . Updating the patch.

    All other deprecations are from here: 📌 PHP 8.2 - Fix deprecated dynamic properties Fixed

  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 7.4 & MySQL 8
    last update over 1 year ago
    47 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    47 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 7.x + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    47 pass, 7 fail
  • 🇸🇰Slovakia poker10

    @TR can we please move this somehow? This is blocking usability of the module on PHP 8.2 and also Rules module. Thanks!

    • TR committed 3fef11d8 on 7.x-1.x authored by poker10
      Issue #3328049 by poker10: PHP 8.2 - Fix deprecated dynamic properties
      
  • Status changed to Fixed over 1 year ago
  • 🇺🇸United States tr Cascadia

    Committed. Thanks. I was hoping for at least one review, because changes to the 7.x-1.x branch can affect a lot of legacy sites. But I don't think the changes in the patch have any potential to break things, so I went ahead and committed this as-is.

    If you want to submit a patch for Rules, I can commit that as well.

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    Thanks. It would be helpful to update the automated testing configuration as well to add PHP 8.2.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • 🇨🇦Canada awasson

    @TR,
    The patch definitely works which is great (thanks @poker10) but is there any reason a new release hasn't been tagged? For now I'll use the Dev but it's a production site on PHP 8.2 so I'd rather use stable releases.

    Cheers,
    Andrew

  • 🇨🇦Canada joseph.olstad

    In addition to a tagged release with PHP 8.2 compatibility,
    We also still need an updated testing configuration to add PHP 8.2

  • 🇨🇦Canada joseph.olstad

    @TR, we appreciate the commit, could you please also tag a PHP 8.2 compatible release and also update the testing configuration to add PHP 8.2 ?

  • 🇩🇰Denmark ressa Copenhagen

    The logs are getting filled with "Creation of dynamic property" warnings, and since it looks like the dev-version works well, perhaps it can get released as 8.x-1.5?

Production build 0.71.5 2024