Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-76411

PHP 8.2: Dynamic Properties are deprecated

XMLWordPrintable

    • MOODLE_402_STABLE
    • Hide

      Test are covered by PHP Unit and Behat

      Show
      Test are covered by PHP Unit and Behat
    • 6
    • Team Hedgehog Sprint 1 review, Team Hedgehog Sprint 2.1, Team Hedgehog Sprint 2.2, Team Hedgehog 2023 Sprint 1.3, Team Hedgehog 2023 Sprint 1.4, Team Hedgehog 2023 Sprint 2.0, Team Hedgehog 2023 Sprint 2.1, Team Hedgehog 2023 Sprint 2.2

      From PHP 8.2, if someone tries to set a value for a class property which does not have a definitino, a deprecation notice will be emitted. For example:

      class User {
          private int $uid;
      }
       
      $user = new User();
      $user->name = 'Foo';
      

      Deprecated: Creation of dynamic property User::$name is deprecated in ... on line ...
      

      Also from internally:

      class User {
          public function __construct() {
              $this->name = 'test';
          }
      }
       
      new User();
      

      Deprecated: Creation of dynamic property User::$name is deprecated in ... on line ...
      

      Much more detail here: https://php.watch/versions/8.2/dynamic-properties-deprecated

      We should look at writing a sniff for this if possible.

      Note: For some legacy code, we may choose to work around this using the AllowDynamicProperties attribute:

       #[AllowDynamicProperties]
       class User {
           private int $uid;
       }
       
       $user = new User();
       $user->name = 'Foo';
      

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

      Other cases not affected

      • stdClass
      • classes with a magic __get and __set method

      phpstan configuration

      I used the following phpstan configuration to genreate the above:

      parameters
        # Dynamic properties emits warnings from PHP 8.2 onwards.
        checkDynamicProperties: true
      

      Note: I would recommend generating a baseline first, then adding the above configuration, and then running again against the baseline with the new configuration. This will identify any problem areas.

        There are no Sub-Tasks for this issue.

            meirza.arson@moodle.com Meirza
            dobedobedoh Andrew Lyons
            Raquel Ortega Raquel Ortega
            Votes:
            3 Vote for this issue
            Watchers:
            16 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 2 days, 40 minutes Original Estimate - 2 days, 40 minutes
                2d 40m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 10 weeks, 3 hours, 16 minutes
                10w 3h 16m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.