On 26/10/2020 09:02, Andreas Bittner wrote:
With regards to the JSON looking different when casting to object:
```
json_encode([1,2,3]); // result: [1,2,3]
json_encode((object)[1,2,3]); // result: {"0":1,"1":2,"2":3}
json_encode([]); // result: []
json_encode((object)[]); // result: {}
Thanks, I hadn't thought of that. For cases as trivial as that, we have
JSON_FORCE_OBJECT, but that's no use for creating structures like [{}, {}]
\stdClass is currently needed and widely used as no other structure
exists that offers dynamic property names while still being forcefully
JSON encoded as a JSON object.
We could replace this with another class. Maybe a real map
implementation. But without the replacement being present \stdClass
should be removed.
Just to be clear, *any* object (except for a few with magic behaviour)
can have dynamic property names added. As far as I know, defining "class
stdClass {}" would be completely equivalent to what stdClass currently
does (i.e. nothing).
The only thing that makes stdClass at all special is the built-in
functionality that returns it, notably the "object cast" short-hand for
creating an instance and dynamically setting properties on it. The
*functionality* (if not necessarily the *performance*) of that
short-hand can definitely be replicated with any normal or anonymous
class: https://3v4l.org/fGRBj
Do we already have a comprehensive list of features and use cases for
\stdClass? If not I would offer to compile one to the best of my
knowledge for a separate discussion on how to replace \stdClass if
possible and needed.
I guess you could search the manual for built-in functions and operators
that return stdClass instances. I doubt there are any that explicitly
require them as input.
For userland code, I'm not aware of any scenario where "new stdClass"
could not be replaced by "new class {}", or why a polyfill would be any
more than "if ( ! class_exists('stdClass') ) { class stdClass{} )".
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php