PHPCompatibility / PHPCompatibility/PHPCompatibility
PHP 8.3: Arbitrary static variable initializers
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.3k
- Forks
- 201
- Avg merge
- 3d 18h
- Merged PRs (30d)
- 42
Description
Related to https://github.com/PHPCompatibility/PHPCompatibility/issues/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.
References
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no implementation files or tests. Start by reviewing the existing PHPCompatibility handling for static-variable declarations, then compare the linked PHP 8.2/8.3 examples and RFC; done means the relevant syntax differences are detected consistently, including arbitrary initializers and duplicate declarations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100