PHPCompatibility / PHPCompatibility/PHPCompatibility

PHP 8.3: ReflectionMethod::__construct() replacement

Open
#1,981 2 comments 0 reactions 1 assignee View on GitHub

@afilina is already working on this.

Since Oct 31, 2025.

PHP: 8.3 Status: to be implemented Type: enhancement
Dominant language
PHP
Stars
2.3k
Forks
201
Avg merge
3d 18h
Merged PRs (30d)
42

Description

Related to #1589

Analysis

Recap of existing PHP 8.2 behavior based on the RFC:

class ReflectionMethod
{
    // Signature 1: called with exactly 2 arguments.
    public function __construct(object|string $objectOrClass, string $method) {}
    
    // Signature 2: called with exactly 1 argument.
    public function __construct(string $classMethod) {}
}

Updated behavior for each version:

class ReflectionMethod
{
    // Signature 1: unchanged behavior
    public function __construct(object|string $objectOrClass, string $method) {}

    // Signature 2: deprecated in 8.4, removed in a future (TBD) major version.
    public function __construct(string $classMethod) {}
 
    // Named constructor: replaces signature 2, added in PHP 8.3
    public static function createFromMethodName(string $method): static {}
}
Top 2000

Found several uses of the deprecated constructor. Examples:

dedoc/scramble:

$reflection = new ReflectionMethod(...explode('@', $route->getAction('uses')));

roave/better-reflection:

return new ReflectionMethod($this->betterReflectionObject->getConstructor());
Detection in PHP 8.2
  • ReflectionMethod::createFromMethodName is invalid.
  • Any form of new ReflectionMethod(...) is unchanged and out of scope for this version, see "Syntax Variations" below.
Detection in PHP 8.3
  • ReflectionMethod::createFromMethodName is valid.
  • Any form of new ReflectionMethod(...) is unchanged and out of scope for this version, see "Syntax Variations" below.
Detection in PHP 8.4
  • new ReflectionMethod(...) with 1 argument is deprecated, add warning. See "Syntax Variations" below.
Syntax Variations
  • ReflectionMethod::createFromMethodName ✅ Can be handled by PHPCompatibility.
  • new ReflectionMethod(...) ✅ Can be handled by PHPCompatibility.
  • $className = 'ReflectionMethod'; new $className(); ❌ Unreliable, as $className could be defined anywhere.
  • new SomeClassWhichExtendsReflectionMethod(...) ❌ Can't be handled.
  • class Foo extends ReflectionMethod { ... parent::__construct(...) ... } ✅ Should be doable.
3v4l.org

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.