website/errors/property.readOnlyByPhpDocAssignByRef.md
<?php declare(strict_types = 1);
class Foo
{
/**
* @var int
* @readonly
*/
public $value;
public function __construct(int $value)
{
$this->value = $value;
}
public function doFoo(): void
{
$ref = &$this->value; // ERROR: @readonly property Foo::$value is assigned by reference.
}
}
A property marked with the @readonly PHPDoc annotation is being assigned by reference. When a variable holds a reference to a property, the property's value can be changed through that reference, which violates the @readonly contract. Even if the code does not intend to modify the property through the reference, the reference itself creates a pathway for mutation.
Read the property value into a regular variable instead of creating a reference:
<?php declare(strict_types = 1);
class Foo
{
public function doFoo(): void
{
- $ref = &$this->value;
+ $ref = $this->value;
}
}
If the property should actually be mutable, remove the @readonly annotation:
<?php declare(strict_types = 1);
class Foo
{
/**
* @var int
- * @readonly
*/
public $value;
}