rfc:static_variable_inheritance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:static_variable_inheritance [2021/02/25 09:03] – Mention non-static methods nikicrfc:static_variable_inheritance [2021/04/28 11:06] nikic
Line 2: Line 2:
   * Date: 2021-02-23   * Date: 2021-02-23
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * Status: Accepted
   * Target Version: PHP 8.1   * Target Version: PHP 8.1
 +  * Implementation: https://github.com/php/php-src/pull/6719
  
 ===== Introduction ===== ===== Introduction =====
Line 185: Line 186:
 </PHP> </PHP>
  
-This is consistent with the general semantics of traits as "compiler-assisted copy and paste". The code behaves as-if the method definition was literally pasted into both classes. This is also consistent with the behavior of static properties in traits.+This is consistent with the general semantics of traits as "compiler-assisted copy and paste". The code behaves as-if the method definition was literally pasted into both classes. This is also consistent with the behavior of static properties in traits. Additionally, it ensures that if a method is used multiple times under different aliases, they will all have distinct static variables.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-The behavior of static variables in methods changes as described above. The current behavior of static variables is not documented. I consider it to be borderline buggy.+The behavior of static variables in methods changes as described above. The current behavior of static variables is not documented. I consider it to be borderline buggy. Typical memoization use-cases for static variables will not be affected (apart from memoizing more effectively). 
 + 
 +Code that intentionally relies on the current behavior can be made compatible by indexing the static variable by the class name: 
 + 
 +<PHP> 
 +class A { 
 +    public function counter() { 
 +        // This code works both for static and non-static methods. 
 +        static $counters = []; 
 +        $counters[static::class] ??= 0; 
 +        return ++$counters[static::class]; 
 +    } 
 +
 +class B extends A { 
 +
 + 
 +var_dump((new A)->counter()); // int(1) 
 +var_dump((new A)->counter()); // int(2) 
 +var_dump((new B)->counter()); // int(1) 
 +var_dump((new B)->counter()); // int(2) 
 +</PHP> 
 + 
 +This code will work the same way both before and after the proposed behavior change.
  
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Voting started 2021-04-14 and ended 2021-04-28. 
 + 
 +<doodle title="Change static variable inheritance as proposed?" auth="nikic" voteType="single" closed="true"> 
 +   Yes 
 +   No 
 +</doodle>
rfc/static_variable_inheritance.txt · Last modified: 2021/04/28 15:12 by nikic