On Tue, 17 Sep 2019 at 00:21, Larry Garfield <la...@garfieldtech.com> wrote: > [..] > I think everyone's in agreement about: > > 1) Making objects easier to work with. > 2) Devs should use objects more. > > (Side note: I have an article submitted to php[architect] entitled "Never* > use arrays" that goes into this topic in much more detail. I am 100% on > board with getting developers using objects more and arrays less.) > > However, why do we want devs to use objects more? If we just want the > arrow-like syntax, then this works today: > > $val = (object)[ > 'foo' => 'bar', > 'baz' => 5, > ]; > > And now developers are using an anonymous "object". However, that offers > basically no meaningful improvement except funnier passing semantics.
I would argue they can even be inferior to arrays. Arrays have a big advantage over anonymous object, or even over any mutable object: They can be passed "by value", which protects them against pollution / poisoning. Objects are always passed as an instance pointer (I am not going to say by reference here), which means they can always be modified in the called function, unless they have been actively cloned beforehand. Even classes that are designed to be immutable can still be poisoned by setting anonymous properties. Perhaps a future RFC could suggest a syntax to prevent that from happening :) There are some other advantages of arrays, but they depend a lot on the use case: - Array syntax is more smooth for some use cases. - Arrays can be counted, and compared vs the empty array. - A type hint "array" excludes anything else. A type hint "\stdClass" still allows any subclass of \stdClass, which could have its own methods. Otherwise, I agree: Objects with defined classes have many advantages vs arrays or anonymous objects. Depends on the use case of course. Arrays are still good for mapping arbitrary keys to arbitrary values. -- Andreas > > As I see it, the advantages of using objects over arrays are two fold: > > 1) The PHP engine is able to make dramatic optimizations when working with > *defined* classes with *defined* properties. I think (although have not > verified) that anonymous classes get the same benefit but iff their > properties are explicitly defined like any other class. > > 2) They're more self-documenting and statically analyzable (by your IDE or > any other tool)... but that's true only if you have an explicitly defined > class! > > So I see really no advantage whatsoever in replacing this: > > function doSomething(int $a, array $options = []) { ... } > > with this: > > function doSomething(int $a, object $options = {}) { ... } > > It's only an advantage if I do this: > > function doSomething(int $a, SomethingOptions $options = null) { ... } > > Doing that has many advantages, I think we all agree. But going halfway > doesn't give us any benefits other than swapping [' '] for ->. > > What we want to do is make it easier to create $options inline in a > self-documenting way. I'm all for that. But that first requires the > developer still define SomethingOptions somewhere. > > So for me, the entire "easier to use anonymous one-off classes" argument > falls apart, because there's no material benefit there other than saying "but > now it's using objects". For that, as noted, we already have an ample syntax > that accomplishes just as much. > > So rather than making it easier for people to make "anonymous structs that > use object syntax", we should be making it easier for people to define > for-realsies named classes and using them, because *that* is where the > benefit comes from. > > And for that part of the problem (making named struct-like classes easier to > work with), as noted in the other thread I find the named > parameters/auto-promotion combination to solve the problem just as well, and > then some. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php