-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Readonly properties mutable when assigning reference to them #7942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
However one must note that the following example works as intended: <?php // lint >= 8.1
namespace ReadonlyPropertyPassedByRef;
class Foo
{
public readonly int $bar;
public function set(int &$i)
{
$this->bar = &$i;
}
}
$i = 1;
$z = 25;
$foo = new Foo();
$foo->set($i);
$i = &$z;
var_dump($foo->bar); The reference of the variable has been set as read only, not it's value. |
Not sure whether this is a bug; it might fall in the "readonly properties do not preclude interior mutability" category. |
/cc @nikic The engine does prevent creating references from Maybe we could do the same in the other direction. class Foo {
public readonly int $bar;
public function __construct(int &$bar) {
// Error right here, can't assign reference
$this->bar = &$bar;
}
}
$i = 42;
$foo = new Foo($i);
$i++;
var_dump($foo->bar); |
@cmb69 I’d consider it a bug because otherwise this sentence from the RFC isn’t really true:
|
Yeah, this should not be allowed. |
In zend_std_get_property_ptr_ptr(), we currently allow acquiring a property pointer if the current value is undef and the scope has initialization access. This then allows zend_assign_to_property_reference() to assign a reference to the property. |
Always falling back to read_property for readonly props means that |
Thank you @iluuu1994! |
Description
Readonly properties should not be mutable with any means, but the following code surprisingly works (https://3v4l.org/qiDTk):
Additionally, without the
&
inint &$i
, the property value stays1
but does not report any error: https://3v4l.org/NMDApPHP Version
PHP 8.1
Operating System
No response
The text was updated successfully, but these errors were encountered: