Menu

← Back to property.*

Error Identifier: property.readOnlyAssignNotOnThis

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 $bar;

	public function __construct(int $bar)
	{
		$self = new self(1);
		$self->bar = $bar;
	}
}

Why is it reported? #

PHP’s readonly properties can only be initialized once, and only from within the scope of the declaring class’s constructor. Additionally, the initialization must happen on $this – not on any other instance of the class. Assigning a readonly property on a different instance (even of the same class) inside the constructor is not allowed.

In the example above, $self->bar = $bar assigns the readonly property on a newly created $self instance rather than on $this, which violates the readonly property contract.

How to fix it #

Assign the readonly property on $this instead of on another instance:

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

 	public function __construct(int $bar)
 	{
-		$self = new self(1);
-		$self->bar = $bar;
+		$this->bar = $bar;
 	}
 }

Or pass the value through the constructor of the other instance so each instance initializes its own readonly property:

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

 	public function __construct(int $bar)
 	{
-		$self = new self(1);
-		$self->bar = $bar;
+		$this->bar = $bar;
+		$self = new self(1); // $self->bar is set via its own constructor
 	}
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Properties\ReadOnlyPropertyAssignRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.