Menu

← Back to property.*

Error Identifier: property.readOnlyByPhpDocAssignNotOnThis

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
{
	/** @readonly */
	public int $value;

	public function __construct(self $other)
	{
		$other->value = 10; // ERROR: @readonly property Foo::$value is not assigned on $this.
	}
}

Why is it reported? #

The property is marked as @readonly in its PHPDoc, which means it should only be assigned once during initialization. While PHPStan allows @readonly properties to be assigned inside the constructor of the declaring class, the assignment must be on $this – not on another instance of the same class. Assigning a @readonly property on a different object instance violates the readonly contract because it modifies state that may have already been initialized.

How to fix it #

Assign the @readonly property on $this inside the constructor:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	/** @readonly */
 	public int $value;

-	public function __construct(self $other)
+	public function __construct(int $value)
 	{
-		$other->value = 10;
+		$this->value = $value;
 	}
 }

Alternatively, if the property should be writable on different instances, remove the @readonly annotation:

 <?php declare(strict_types = 1);
 
 class Foo
 {
-	/** @readonly */
 	public int $value;

 	public function __construct(self $other)
 	{
 		$other->value = 10;
 	}
 }

How to ignore this error #

You can use the identifier property.readOnlyByPhpDocAssignNotOnThis to ignore this error using a comment:

// @phpstan-ignore property.readOnlyByPhpDocAssignNotOnThis
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: property.readOnlyByPhpDocAssignNotOnThis

Rules that report this error #

  • PHPStan\Rules\Properties\ReadOnlyByPhpDocPropertyAssignRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.