Menu

← Back to assign.*

Error Identifier: assign.readOnlyProperty

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);

class Foo
{
	private readonly int $value;

	public function __construct()
	{
		$this->value = 1;
		$this->value = 2;
	}
}

Why is it reported? #

A readonly property in PHP can only be assigned once. After its initial assignment, any further attempt to assign a value to it will cause a runtime error. PHPStan detects when a readonly property is assigned more than once in the constructor or across initialization paths.

In the example above, $this->value is assigned 1 and then immediately overwritten with 2. Since the property is declared as readonly, the second assignment is invalid.

How to fix it #

Remove the duplicate assignment and keep only the intended one:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	private readonly int $value;

 	public function __construct()
 	{
-		$this->value = 1;
 		$this->value = 2;
 	}
 }

Or use conditional logic to assign the property only once:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	private readonly int $value;

-	public function __construct()
+	public function __construct(bool $flag)
 	{
-		$this->value = 1;
-		$this->value = 2;
+		$this->value = $flag ? 1 : 2;
 	}
 }

How to ignore this error #

You can use the identifier assign.readOnlyProperty to ignore this error using a comment:

// @phpstan-ignore assign.readOnlyProperty
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: assign.readOnlyProperty

Rules that report this error #

  • PHPStan\Rules\Properties\MissingReadOnlyPropertyAssignRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.