website/errors/new.resultUnused.md
<?php declare(strict_types = 1);
class Validator
{
/** @phpstan-pure */
public function __construct()
{
}
}
function doFoo(): void
{
new Validator(); // ERROR: Call to new Validator() on a separate line has no effect.
}
A new ClassName() expression appears as a standalone statement but the constructor has no side effects. The created object is not assigned to a variable, returned, or used in any way. Since the constructor is pure (or has no impure points), instantiating the object without using it has no observable effect and is likely a mistake.
Use the result of the instantiation:
<?php declare(strict_types = 1);
function doFoo(): void
{
- new Validator();
+ $validator = new Validator();
}
If the constructor is intended to have side effects, make sure the constructor actually performs a side effect so PHPStan recognizes it as impure:
<?php declare(strict_types = 1);
class Validator
{
- /** @phpstan-pure */
public function __construct()
{
+ // perform some side effect
}
}