Cannot assign %s to reference held by property %s::$%s of type %s

Description

When a variable holds a reference to a property, it also has to satisfy the type constraints. Here, by using a typed reference, the $var variable looks like another local variable, but it is in fact typed.

Example

<?php

class X {
    public static int $int = 1;
}

$var = &X::$int;

$var = 'abc';
?>

Literal Examples

  • Cannot assign string to reference held by property X::$int of type int

Solutions

  • Remove the type from the property.

  • Use a valid value to the property.

  • Remove the initial link to the property until the value is of the correct type.