PHP 8.2: unexpected deprecation for dynamic property set via magic method
Description
The following code:
<?php
class Foo {
public function __set( $name, $value ) {
$this->$name = $value;
}
}
$obj = new Foo();
$obj->test = 'test';
Resulted in this output:
Deprecated: Creation of dynamic property Foo::$test is deprecated in /in/Ds0gN on line 5
https://3v4l.org/Ds0gN/rfc#vgit.master
But I expected this output instead:
No error
I realize that this is an "unconventional" interpretation of how magic methods should be implemented, but at the same time, the RFC explicitly excluded dynamic properties being set via magic methods from the deprecation, so I'd really like to invite some discussion on this.
PHP Version
8.2/nightly
Operating System
No response
Honestly, I didn't even know the engine protects you from infinite recursion in this case.
The current behavior seems reasonable to me. I'm assuming (but maybe @nikic can correct me on this one) the RFC refers to the fact that triggering __set through undefined properties won't also emit a "Creation of dynamic property" deprecation. As properties are supposed to be dynamic here that seems like the perfect use case for #[AllowDynamicProperties].
When an inaccessible property is accessed recursively in a magic __get/__set accessor, it will bypass magic __get/__set and access the property directly instead. This may either be an access to a declared property or to a dynamic property. If that ends up creating a dynamic property, a deprecation warning will be thrown.