Skip to content

Passing an anonymous class instance to the constructor of another anonymous class errors #11511

@MidnightDesign

Description

@MidnightDesign

Bug report

The following snippet results in an error:

<?php declare(strict_types = 1);

$myObject = new class (new class { public string $bar = 'test'; }) {
	public function __construct(public object $foo)
	{
	}
};
echo $myObject->foo->bar;

Property $bar was not found in reflection of class AnonymousClass6bf955dcd48a8ffb510ed90de4f268e1.

Code snippet that reproduces the problem

https://phpstan.org/r/2bd39098-083e-4a24-9d0b-e2ea92289a0b

Expected output

Expected no error

Additional info

Extracting the passed object into a variable first works. (I had to add the object{bar: string} type annotation in this case for the last line not to error, but that doesn't have anything to do with the issue itself.)

<?php declare(strict_types = 1);

$foo = new class { public string $bar = 'test'; };
$myObject = new class ($foo) {
	/**
	 * @param object{bar: string} $foo
	 */
	public function __construct(public object $foo)
	{
	}
};
echo $myObject->foo->bar;

https://phpstan.org/r/76b2f2d7-1960-44f6-9a5a-44609d3335e4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions