Parameter $%s of %s hook %s::$%s must not be pass-by-reference

Description

The parameter of a set property hook receives the value being assigned to the property. It cannot be declared as pass-by-reference with a leading &, since a property hook is not meant to modify the caller’s variable; it only reacts to, or transforms, the value on its way into the property.

Example

<?php

class X
{
    public string $property {
        set(&$value) {
            $this->property = $value;
        }
    }
}

?>

Literal Examples

  • Parameter $value of set hook X::$property must not be pass-by-reference

Solutions

  • Remove the & from the hook’s parameter.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.