website/errors/new.static.md
<?php declare(strict_types = 1);
class Foo
{
public function create(): static
{
return new static();
}
}
Using new static() in a non-final class is unsafe because child classes may override the constructor with different parameters or requirements. When new static() is called, it creates an instance of the actual runtime class (which may be a subclass), but uses the parent's constructor call. If a child class changes the constructor signature, this will break.
Learn more: Solving PHPStan error "Unsafe usage of new static()"
Mark the class as final if it is not meant to be extended:
-class Foo
+final class Foo
{
public function create(): static
{
return new static();
}
}
Or mark the constructor as final to prevent subclasses from changing its signature:
class Foo
{
+ final public function __construct()
+ {
+ }
+
public function create(): static
{
return new static();
}
}
Or add @phpstan-consistent-constructor to the class to declare that all subclasses must have a compatible constructor:
+/** @phpstan-consistent-constructor */
class Foo
{
public function create(): static
{
return new static();
}
}