Re: [Discussion] Make objects unpackable by default

From: Date: Thu, 14 Nov 2024 21:27:30 +0000
Subject: Re: [Discussion] Make objects unpackable by default
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-125960@lists.php.net to get a copy of this message
On Thursday, 14 November 2024 at 17:24, Larry Garfield <larry@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 ocramius@gmail.com: > > > > > On Thu, 14 Nov 2024, 11:29 MrMeshok, ilyaorlov124@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

« previous php.internals (#125960) next »