Skip to content

Readonly properties mutable when assigning reference to them #7942

Closed
@ondrejmirtes

Description

@ondrejmirtes

Description

Readonly properties should not be mutable with any means, but the following code surprisingly works (https://3v4l.org/qiDTk):

<?php

class Foo
{
	public readonly int $bar;
	public function set(int &$i)
	{
		$this->bar = &$i;
	}
}

$i = 1;
$foo = new Foo();
$foo->set($i);
$i = 20;
var_dump($foo->bar); // 20

Additionally, without the & in int &$i, the property value stays 1 but does not report any error: https://3v4l.org/NMDAp

PHP Version

PHP 8.1

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions