← All results

new through $object::class

Group: Object compatibility · Category: language · File: objects_class_constant_on_instance.php

$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 results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 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.1PHPStan - PHP Static Analysis Tool 2.3.1 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-rules
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 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
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 diff
Line 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.57.0mago 1.57.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.87.0mir 0.87.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 diff
Line 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.19409phpy 1.0.19409 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 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 diff
Line 32: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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 diff
Line 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]"]

Source

<?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
}