website/errors/doctrine.finalConstructor.md
<?php declare(strict_types = 1);
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class MyEntity
{
final public function __construct(private string $name)
{
}
}
This error is reported by phpstan/phpstan-doctrine.
Doctrine ORM uses proxy objects for lazy loading. These proxies extend the entity class and override the constructor to prevent premature initialization. If the constructor is declared final, Doctrine cannot create a proxy class for this entity, which breaks lazy loading functionality.
This applies to all Doctrine ORM entities (but not embeddables, which are not proxied).
Remove the final keyword from the constructor:
<?php declare(strict_types = 1);
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class MyEntity
{
- final public function __construct(private string $name)
+ public function __construct(private string $name)
{
}
}