PHPCompatibility / PHPCompatibility/PHPCompatibility

PHP 8.3: Arbitrary static variable initializers

Open
#1,986 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

PHP: 8.3 Status: needs investigation Type: enhancement
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: valid
  • function () { 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: error
  • function () { static $i = min(1, 2); } Arbitrary expression: valid
Syntax Variations & Detectability

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.