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

Persistent class uses static cache incorrectly, breaks under PHP8.1

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.11.4, 4.0
    • 3.11.5
    • Other

    Description

      Class core\persistent has a function

      final public static function properties_definition() {
          static $def = null;
      

      that uses static variable for caching the properties definitions that is supposed to be cached for the current class. It works in PHP7 and PHP8.0, but it breaks in PHP8.1 and I personally find the new behavior more logical.

        . When a method using static variables is inherited (but not overridden), the
          inherited method will now share static variables with the parent method.
       
              class A {
                  public static function counter() {
                      static $counter = 0;
                      $counter++;
                      return $counter;
                  }
              }
              class B extends A {}
       
              var_dump(A::counter()); // int(1)
              var_dump(A::counter()); // int(2)
              var_dump(B::counter()); // int(3), previously int(1)
              var_dump(B::counter()); // int(4), previously int(2)
       
          This means that static variables in methods now behave the same way as
          static properties.
      

      RFC https://wiki.php.net/rfc/static_variable_inheritance

      Attachments

        Issue Links

          Activity

            People

              marina Marina Glancy
              marina Marina Glancy
              Paul Holden Paul Holden
              Eloy Lafuente (stronk7) Eloy Lafuente (stronk7)
              CiBoT CiBoT
              Adrian Greeve, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                17/Jan/22

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 2 hours, 5 minutes
                  2h 5m