Menu

← Back to property.*

Error Identifier: property.assignByRef

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 User
{

	public private(set) string $name = 'John';

}

$user = new User();
$ref = &$user->name;

Why is it reported? #

A property with restricted write visibility (such as private(set) or protected(set), introduced in PHP 8.4 asymmetric visibility) is being assigned by reference. Assigning by reference creates an alias to the property, which would allow modifying the property value through the reference from outside the allowed scope, bypassing the visibility restriction.

How to fix it #

Assign the property value directly instead of by reference:

 <?php declare(strict_types = 1);
 
 class User
 {

 	public private(set) string $name = 'John';

 }

 $user = new User();
-$ref = &$user->name;
+$value = $user->name;

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Properties\PropertyAssignRefRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.