Menu

← Back to property.*

Error Identifier: property.readOnlyAssignByRef

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
{
	public function __construct(
		public readonly int $value = 42,
	) {}

	public function doFoo(): void
	{
		$ref = &$this->value;
	}
}

Why is it reported? #

A readonly property cannot be assigned by reference. Creating a reference to a readonly property would allow its value to be modified through the reference variable, bypassing the readonly constraint. PHP prohibits this at the language level.

How to fix it #

Copy the value instead of creating a reference:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	public function __construct(
 		public readonly int $value = 42,
 	) {}

 	public function doFoo(): void
 	{
-		$ref = &$this->value;
+		$copy = $this->value;
 	}
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Properties\ReadOnlyPropertyAssignRefRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.