Menu

← Back to class.*

Error Identifier: class.nonReadOnly

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

readonly class ParentClass
{
}

class ChildClass extends ParentClass
{
}

Why is it reported? #

A non-readonly class cannot extend a readonly class. PHP enforces this constraint to maintain the readonly invariant across the inheritance chain. When a class is declared as readonly, all its properties are implicitly readonly, and this guarantee must hold for all subclasses as well. Allowing a non-readonly child class would break this invariant because the child could introduce mutable properties.

In the example above, ChildClass is not readonly but extends the readonly ParentClass, which violates this rule.

How to fix it #

Make the child class readonly as well:

 <?php declare(strict_types = 1);
 
 readonly class ParentClass
 {
 }

-class ChildClass extends ParentClass
+readonly class ChildClass extends ParentClass
 {
 }

Or remove the readonly modifier from the parent class if mutability is needed:

 <?php declare(strict_types = 1);
 
-readonly class ParentClass
+class ParentClass
 {
 }

 class ChildClass extends ParentClass
 {
 }

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Classes\ExistingClassInClassExtendsRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.