-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array access for higher-order collection operations #16313
Conversation
/cc @alexbowers |
This feels really dirty, implementing only a single method required by an interface. (It's very prevalent in other languages, but ugh). I much prefer what we have now, and using it is also much nicer than array syntax. |
Yeah, I prefer the current implementation. |
@JosephSilber To be honest, I'm somewhat irritated about that feedback. How is this in any way "dirty" - if anything, the whole feature is dirty. This just follows the implementation of the method and object access proxy feature, and does what we have to do to enable array access - that's a normal PHP feature. As I've explained in #16274, this is about expectability. If you have objects in your collection, you can access their properties like object properties, and object methods like object methods through the proxy (as if you're dealing with one object, even though it is a whole collection); if you have a collection of arrays, what would be more straightforward then accessing them like you would access a normal array? |
Honestly, there's zero "expectability" here. This is a beautifully magical feature, and you learn to use it the way it was designed.
We've actually had many requests from people to add |
Doesn't matter whether you call it expectability or consistency. There is a lot of magic going on here, true - that makes it even more important to be consistent with the rest of the language in as many ways as we can - in this case in the way we access the collection items. Anyway, how do we access array elements with names that aren't allowed as property names now? $collection->map->field-name; // doesn't work
$collection->map['field-name']; // would work |
For those rare cases, you can use curly braces: $collection->map->{'field-name'}; |
I advise to implement real array access for collection items that are arrays, as replacement for the magical #16274.
This PR implements that. :)