> On Sep 8, 2021, at 4:59 PM, Rowan Tommins <rowan.coll...@gmail.com> wrote: >> 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.
The concern is *not* were they are not needed, but instead where they *are* needed. Such as when loading JSON from a source that does not ensure the schema is 100% stable over time. That's also why I was hoping some of those complaining about deprecating stdClass would explain explain their use-cases as they may know of use-cases we are not considering. > 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). Exactly! Actually the same functionality for named classes is something I have wanted since literally the very first day I starting using PHP so I wouldn't have to run userland code that loops through an array that runs for so many object instantiations. I have to believe it would be more performant if in C. (Reasons I do that? Loading from JSON, XML and/or SQL.). We've need either a magic method or ability to pass a close for name translations and short circuiting. That functionality is the primary reason I almost always use a base class; it would be nice to get rid of that requirement. > 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. How do you define "initial?" -Mike -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php