I do know about get_object_vars, but for me foreach ($object as $prop) {} looks nicer and a little bit faster (probably because there are no function calls). Also it's more flexible because in future you can implement an Iterator on class and alter iteration behavior. But after reading through https://externals.io/message/103718 and https://github.com/phpstan/phpstan/issues/1060 I can see how this behavior can lead to bugs if the developer didn't intend the object to be iterated. So a better approach would be to add getIterator to all my DTOs ```php public function getIterator(): Traversable { return new ArrayIterator($this); } ``` But ArrayIterator is a lot slower than simple object iteration (2.4 times slower with only public properties in the object and 1.5 times slower with some private/protected properties). It would be nice if there was some lightweight ObjectIterarotor designed to work with object properties. Also some note in docs to discourage usage of object iteration would be good https://www.php.net/manual/en/language.oop5.iterations.php
пт, 15 нояб. 2024 г. в 02:30, Gina P. Banyard <intern...@gpb.moe>: > > On Thursday, 14 November 2024 at 17:24, Larry Garfield > <la...@garfieldtech.com> wrote: > > > On Thu, Nov 14, 2024, at 7:37 AM, Christian Schneider wrote: > > > > > Am 14.11.2024 um 10:59 schrieb Marco Pivetta ocram...@gmail.com: > > > > > > > On Thu, 14 Nov 2024, 11:29 MrMeshok, ilyaorlov...@gmail.com wrote: > > > > > > > > > Hello, Internals! > > > > > > > > > > As you know if you try to unpack a regular object (`...$object`) you > > > > > will get an error: Only arrays and Traversables can be unpacked. > > > > > I don't really see a reason for this restriction, because foreach on > > > > > objects works perfectly fine, so I made a feature request on GitHub > > > > > to change this behavior (https://github.com/php/php-src/issues/16038) > > > > > and was advised to see opinions on this here. > > > > > So, what do you think? And does this change require RFC? > > > > > > > > TBH, foreach on objects is already quite an abomination in itself 😬 > > > > > > It is a bit less weird if you use value objects or json data in object > > > form. > > > I would be careful to judge how other people use the language, just > > > saying ;-) > > > > > > - Chris > > > > > > Judging how one should use a language is a core component of language > > design. > > > > I'm with Marco on this one, and I'd be more than happy to get rid of > > foreach($object), too. It's way, way more complicated a topic than you > > think. > > > > How does visibility play into it? > > Should hooks be invoked or no? > > Do references play into anything? > > > > Those are some of the questions we had to deal with for hooks and > > foreach(). It's non-trivial. You'd need to address all the same edgy issues > > here, too. > > > > And that's assuming it's even a good idea, which I don't think it is. If > > you are using a value object to define unordered arguments, just pass the > > value object itself. If the receiving function can't handle that, then it > > is simple enough that you don't need a value object. If that's not the > > case, then your code design is bad and the language should not try to paper > > over that for you. Either simplify the function or adapt it to use a value > > object directly. (With union types you can even do so in a BC fashion much > > of the time.) > > Agreed, this bites us constantly when needing to reason about what an > "object" is in PHP. > Similar to array it mixes the concept of a struct, with that of a reference > value, and "overloading" various behaviours (e.g. iterators and ArrayAccess). > It is *extremely* simple to go from foreach ($object as $prop) {} to > forearch(get_object_vars($object) as $prop) {} > and mandating this would resolve a lot of complexity, be that from the > perspective of what is "possible" or not to do, and from an engine PoV. > > > Best regards, > > Gina P. Banyard