-
-
Notifications
You must be signed in to change notification settings - Fork 205
Open
Description
Related to #1589
Analysis
Summary of existing PHP 8.2 behavior based on the RFC:
// Constant expressions can be assigned in this context (exact meaning in Syntax Variations below).
function foo1() {
static $i = 1;
}
// Can redeclare, with the last taking precedence.
function foo2() {
static $i = 1;
static $i = 2;
}
// getStaticVariables() is able to get the literal value.
var_dump((new ReflectionFunction('foo2'))->getStaticVariables()['i']); // int(2)Updated behavior in PHP 8.3:
// Allows arbitrary expressions (exact meaning in Syntax Variations below).
function foo1() {
static $i = min(1, 2);
}
// Re-declaring is now a fatal error.
function foo2() {
static $i = min(1, 2);
static $i = max(1, 2); // Fatal error: Duplicate declaration of static variable $i
}
// getStaticVariables() can get the expression's value or null if not initialized. However, since the above syntax
// is a fatal error in PHP 8.2, there is no observable difference, so we don't need to sniff getStaticVariables().
var_dump((new ReflectionFunction('foo2'))->getStaticVariables()['i']); // int(2)Everything in the "Other semantics" section of the RFC has no impact on what we need to sniff. Either the syntax exists or it doesn't.
- Exceptions during initialization
- Destructor
- Recursion
Top 2000 Packages
Found quite a few usages in the top 2000 packages ("static $" in a function). See results.
Detection in PHP 8.2
function () { static $i; }Without assignment: valid.function () { static $i = 1; }Constant expression: valid.function () { static $a = 1; static $a = 2; }Duplicate declaration: validfunction () { static $i = min(1, 2); }Arbitrary expression: error
Detection in PHP 8.3
function () { static $i; }Without assignment: valid.function () { static $i = 1; }Constant expression: valid.function () { static $a = 1; static $a = 2; }Duplicate declaration: errorfunction () { static $i = min(1, 2); }Arbitrary expression: valid
Syntax Variations & Detectability
- Expressions valid in both versions
- Expressions valid only in PHP 8.3 - uncomment one at a time to test the fatal error in 8.2
- Static outside of functions
- Duplicate declaration - re-declaring is a fatal error since PHP 8.3.
- Interaction with ReflectionFunction::getStaticVariables() - when using syntax supported in both versions, no difference can be observed.
- More PHP 8.3 from @jrfnl
- Multi-declarations from @jrfnl
function () { static $i; }✅ Without assignment: can be handled by PHPCompatibility.function () { static $a = 1; static $a = 2; }✅ Duplicate declaration in the same function: can be handled by PHPCompatibility.function () { static $i = 1; }✅❌ Various expressions: I identified 52 expression types in the first 2 samples above. I will rely on @jrfnl's knowledge of the tool's capabilities.