website/errors/impure.die.md
<?php declare(strict_types = 1);
class Foo
{
/** @phpstan-pure */
public function doFoo(): string
{
die('fatal error');
}
}
The die language construct is used inside a function or method marked as @phpstan-pure. Pure functions must not have side effects -- they should only compute and return a value based on their inputs. Calling die terminates the entire PHP process, which is a significant side effect.
Remove die from the pure function by throwing an exception instead, or remove the @phpstan-pure annotation if the function genuinely needs to terminate the process:
<?php declare(strict_types = 1);
class Foo
{
/** @phpstan-pure */
public function doFoo(): string
{
- die('fatal error');
+ throw new \RuntimeException('fatal error');
}
}
Or remove the purity annotation:
<?php declare(strict_types = 1);
class Foo
{
- /** @phpstan-pure */
public function doFoo(): string
{
die('fatal error');
}
}