new through a class-string<T> propertyclass-string<T> held in a property:
new ($this->class)() is a T. The class-string generic case is already
covered for parameters (phpdoc_advanced_generic_class_string); this asks
whether the property read and the new (expr) syntax carry it too.
References:
- PHPStan / Psalm class-string<T>
- https://wiki.php.net/rfc/variable_syntax_tweaks (new with an expression)| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 | Enforced | generics_class_string_property_new.php:39: Call to undeclared method \Conformance\Tests\GenericsClassStringPropertyNew\User::missing [PhanUndeclaredMethod]
|
| phpstan | 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 | Enforced reported Lv.2+ | generics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing(). [reported-from-level=2] generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10] With strict-rulesgenerics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing(). generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
|
| psalm | 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08 next: 7.0.4 |
Enforced | generics_class_string_property_new.php:36: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] generics_class_string_property_new.php:38: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] generics_class_string_property_new.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] generics_class_string_property_new.php:39: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing does not exist [UndefinedMethod]
|
| mago | 1.59.0mago 1.59.0 | Enforced | generics_class_string_property_new.php:39: Method `missing` does not exist on type `Conformance\Tests\GenericsClassStringPropertyNew\User`. [non-existent-method] generics_class_string_property_new.php:39: The first value for `echo` is too general. [mixed-argument]
|
| mir | 0.91.0mir 0.91.0 | Enforced | generics_class_string_property_new.php:39: UndefinedMethod: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing() does not exist [MIR0004]
|
| phpantom | 0.11.1phpantom_lsp 0.11.1 | Enforced | generics_class_string_property_new.php:37: Cannot verify method 'name' — type of '$user' could not be resolved [unresolved_member_access] generics_class_string_property_new.php:39: Cannot verify method 'missing' — type of '$made' could not be resolved [unresolved_member_access]
Expectation diffLine 37: Expected valid value to be accepted, got ["Cannot verify method 'name' — type of '$user' could not be resolved [unresolved_member_access]"] |
| intelephense | 1.18.5intelephense 1.18.5 | Enforced | generics_class_string_property_new.php:39: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006] generics_class_string_property_new.php:39: Undefined method 'missing'. [P1013]
|
| phpactor | 2026.10.07.0Phpactor 2026.10.07.0 | Enforced | generics_class_string_property_new.php:39: Method "missing" does not exist on class "Conformance\Tests\GenericsClassStringPropertyNew\User" [missing_member]
|
| phpy | 1.0.19490phpy 1.0.19490 | Enforced | generics_class_string_property_new.php:39: Call to unknown method: Conformance\Tests\GenericsClassStringPropertyNew\User::missing()
|
| qodana | 262.11879.104PhpStorm 262.11879.104 | Not enforced | No diagnostics reported.
Expectation diffLine 39: Expected 1 error(s) |
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Not enforced | No diagnostics reported.
Expectation diffLine 39: Expected 1 error(s) |
| steins | 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) | Not enforced | No diagnostics reported.
Expectation diffLine 39: Expected 1 error(s) |
| phpstan-strict | 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 | Enforced | generics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing(). generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
|
| psalm-next | 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f | Enforced | generics_class_string_property_new.php:36: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] generics_class_string_property_new.php:38: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] generics_class_string_property_new.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] generics_class_string_property_new.php:39: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing does not exist [UndefinedMethod]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\GenericsClassStringPropertyNew;
/**
* `new` through a `class-string<T>` property.
*
* Instantiating through a `class-string<T>` held in a property:
* `new ($this->class)()` is a T. The class-string generic case is already
* covered for parameters (phpdoc_advanced_generic_class_string); this asks
* whether the property read and the `new (expr)` syntax carry it too.
*
* References:
* - PHPStan / Psalm class-string<T>
* - https://wiki.php.net/rfc/variable_syntax_tweaks (new with an expression)
*/
final class User
{
public function name(): string
{
return 'user';
}
}
final class Factory
{
/** @var class-string<User> */
public string $class = User::class; // T: class-string<User>
}
function instantiates(Factory $factory): void
{
$user = new ($factory->class)(); // E?[noise]: UnsafeInstantiation
echo $user->name(); // V
$made = new ($factory->class)(); // E?[noise]: UnsafeInstantiation
echo $made->missing(); // E: the instance is a User
}