php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP 8.2: unexpected deprecation for dynamic property set via magic method

Open jrfnl opened this issue 4 years ago • 2 comments

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

jrfnl avatar Dec 17 '21 17:12 jrfnl

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].

iluuu1994 avatar Dec 17 '21 17:12 iluuu1994

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.

nikic avatar Dec 17 '21 17:12 nikic