Skip to content

PHP 8.3: ReflectionMethod::__construct() replacement #1981

@afilina

Description

@afilina

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

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

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::createFromMethodNam 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

  • 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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions