Skip to content

Readonly properties mutable when assigning reference to them #7942

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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