new through $object::class$object::class is the class-string of the object's class, so
new ($object::class)() builds another instance of it. A tool that types
$object::class as plain string ends up with object and loses every
member check on the new instance.
References:
- https://wiki.php.net/rfc/class_name_literal_on_object| 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 | Pass | objects_class_constant_on_instance.php:32: Call to undeclared method \Conformance\Tests\ObjectsClassConstantOnInstance\User::missing [PhanUndeclaredMethod] |
| phpstan | 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 | Pass reported Lv.2+ | objects_class_constant_on_instance.php:32: Call to an undefined method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing(). [reported-from-level=2] objects_class_constant_on_instance.php:32: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10] With strict-rulesobjects_class_constant_on_instance.php:32: Call to an undefined method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing(). objects_class_constant_on_instance.php:32: Parameter #1 (mixed) of echo cannot be converted to string. |
| psalm | 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08 next: 7.0.4 |
Fail | objects_class_constant_on_instance.php:30: Cannot safely instantiate class Conformance\Tests\ObjectsClassConstantOnInstance\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] objects_class_constant_on_instance.php:32: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] objects_class_constant_on_instance.php:32: Cannot safely instantiate class Conformance\Tests\ObjectsClassConstantOnInstance\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] objects_class_constant_on_instance.php:32: Method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing does not exist [UndefinedMethod] Expectation diffLine 30: Expected valid value to be accepted, got ["Cannot safely instantiate class Conformance\\Tests\\ObjectsClassConstantOnInstance\\User with \"new $class_name\" as its constructor might change in child classes [UnsafeInstantiation]"] |
| mago | 1.59.0mago 1.59.0 | Pass | objects_class_constant_on_instance.php:32: Method `missing` does not exist on type `Conformance\Tests\ObjectsClassConstantOnInstance\User`. [non-existent-method] objects_class_constant_on_instance.php:32: The first value for `echo` is too general. [mixed-argument] |
| mir | 0.91.0mir 0.91.0 | Pass | objects_class_constant_on_instance.php:32: UndefinedMethod: Method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing() does not exist [MIR0004] |
| phpantom | 0.11.1phpantom_lsp 0.11.1 | Fail | objects_class_constant_on_instance.php:30: Cannot verify method 'name' — type of '$class' could not be resolved [unresolved_member_access] objects_class_constant_on_instance.php:32: Cannot verify method 'missing' — type of '$sameClass' could not be resolved [unresolved_member_access] Expectation diffLine 30: Expected valid value to be accepted, got ["Cannot verify method 'name' — type of '$class' could not be resolved [unresolved_member_access]"] |
| intelephense | 1.18.5intelephense 1.18.5 | Pass | objects_class_constant_on_instance.php:32: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006] objects_class_constant_on_instance.php:32: Undefined method 'missing'. [P1013] |
| phpactor | 2026.10.07.0Phpactor 2026.10.07.0 | Pass | objects_class_constant_on_instance.php:32: Method "missing" does not exist on class "Conformance\Tests\ObjectsClassConstantOnInstance\User" [missing_member] |
| phpy | 1.0.19490phpy 1.0.19490 | Fail | No diagnostics reported. Expectation diffLine 32: Expected 1 error(s) |
| qodana | 262.11879.104PhpStorm 262.11879.104 | Fail | No diagnostics reported. Expectation diffLine 32: 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 | Fail | No diagnostics reported. Expectation diffLine 32: Expected 1 error(s) |
| steins | 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) | Fail | No diagnostics reported. Expectation diffLine 32: Expected 1 error(s) |
| phpstan-strict | 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 | Pass | objects_class_constant_on_instance.php:32: Call to an undefined method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing(). objects_class_constant_on_instance.php:32: Parameter #1 (mixed) of echo cannot be converted to string. |
| psalm-next | 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f | Fail | objects_class_constant_on_instance.php:30: Cannot safely instantiate class Conformance\Tests\ObjectsClassConstantOnInstance\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] objects_class_constant_on_instance.php:32: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] objects_class_constant_on_instance.php:32: Cannot safely instantiate class Conformance\Tests\ObjectsClassConstantOnInstance\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation] objects_class_constant_on_instance.php:32: Method Conformance\Tests\ObjectsClassConstantOnInstance\User::missing does not exist [UndefinedMethod] Expectation diffLine 30: Expected valid value to be accepted, got ["Cannot safely instantiate class Conformance\\Tests\\ObjectsClassConstantOnInstance\\User with \"new $class_name\" as its constructor might change in child classes [UnsafeInstantiation]"] |
<?php
declare(strict_types=1);
namespace Conformance\Tests\ObjectsClassConstantOnInstance;
/**
* `new` through `$object::class`.
*
* `$object::class` is the class-string of the object's class, so
* `new ($object::class)()` builds another instance of it. A tool that types
* `$object::class` as plain string ends up with `object` and loses every
* member check on the new instance.
*
* References:
* - https://wiki.php.net/rfc/class_name_literal_on_object
*/
final class User
{
public function name(): string
{
return 'user';
}
}
function clonesClass(User $user): void
{
$class = $user::class;
echo (new $class())->name(); // V
$sameClass = $user::class;
echo (new $sameClass())->missing(); // E: the new instance is a User
}