On 08/09/2021 16:37, Mike Schinkel wrote:
All future code that needs to refer to the class name will still refer to
`stdClass`, so we won't be gaining much by creating an alias.
Just to be clear, the only code that would need to change is code that
dynamically gets *out* the class name, from get_class(), var_export(),
reflection, and the like. Using the class name in code, like "$foo = new
stdClass;" and "$foo instanceof stdClass", would carry on working just
fine, whichever way we defined the alias.
Besides, don't forget `stdClass::class` in addition to `get_class()`.
The ::class syntax is purely string replacement (apart from some rare
edge cases), and works identically whether the class exists, is an
alias, or doesn't exist at all.
I assume we would also disallow dynamic properties in anonymous classes too,
right? After all, they are just statically declared classes that the developer
do not assign a name.
The difference I see is that stdClass/DynamicObject allows you to add or
remove properties from an object *after it has been created*. I think a
lot of use cases don't actually need that, and would benefit from error
messages when doing so accidentally.
You mentioned short-hand syntaxes like this:
$obj = {
foo: 1,
bar: "hello",
baz: true,
};
I would love for that, or some other short-hand, to be equivalent to this:
$obj = new class(foo: 1, bar: "hello", baz: true) {
public $foo;
public $bar;
public $baz;
public function __construct($foo, $bar, $baz) {
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}
}
That is, an anonymous class, with exactly those three properties. If you
*also* want to be able to define extra properties after it's created,
you could opt into that using whatever mechanism a named class would
(parent class, trait, attribute, etc; see other thread).
Similarly, the objects created by json_decode or PDO_FETCH_OBJECT only
need the *initial* properties to be dynamic, not to allow properties to
be added later.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php