Skip to content
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

Closed

Conversation

franzliedke
Copy link
Contributor

I advise to implement real array access for collection items that are arrays, as replacement for the magical #16274.

This PR implements that. :)

@franzliedke
Copy link
Contributor Author

/cc @alexbowers

@JosephSilber
Copy link
Member

JosephSilber commented Nov 8, 2016

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.

@taylorotwell
Copy link
Member

Yeah, I prefer the current implementation.

@franzliedke
Copy link
Contributor Author

@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?

@JosephSilber
Copy link
Member

this is about expectability

Honestly, there's zero "expectability" here. This is a beautifully magical feature, and you learn to use it the way it was designed.

if you have a collection of arrays, what would be more straightforward than accessing them like you would access a normal array

We've actually had many requests from people to add __get accessors onto the collection, for when the collection contains a single-dimensional associative array (luckily we never did, or we wouldn't have been able to get your delicious higher order operations). Just goes to show people really hate array syntax, and much prefer object syntax (just like the Fluent class).

@franzliedke
Copy link
Contributor Author

franzliedke commented Nov 10, 2016

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

@JosephSilber
Copy link
Member

Anyway, how do we access array elements with names that aren't allowed as property names now?

For those rare cases, you can use curly braces:

$collection->map->{'field-name'};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants