Skip to content

Unexpected error when using ReflectionClass::newInstance() #15032

Closed
phpstan/phpstan-src
#6165

Description

@eliashaeussler

Bug report

As can be seen in below's code snippet, using ReflectionObject::newInstance() (inherited from ReflectionClass::newInstance()) with a template type leads to an error:

Function should return T of object but returns object

Code snippet that reproduces the problem

https://phpstan.org/r/387d0287-c0cb-4aca-86bf-13c4329df80b

Expected output

I wouldn't expect an error here, because the reflection was done on a specific object. Calling newInstance should return a new instance of this exact class (or fail).

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. ondrejmirtes commented on Jul 31, 2026

    @ondrejmirtes
    Member

    We don't have a stub for ReflectionObject at all, should be added.

  2. added this to the Easy fixes milestone on Jul 31, 2026
  3. github-actions commented on Sep 1, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  4. locked as resolved and limited conversation to collaborators on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions