Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Support for objects? #2

Open
jlem opened this issue Apr 9, 2014 · 13 comments
Open

Support for objects? #2

jlem opened this issue Apr 9, 2014 · 13 comments

Comments

@jlem
Copy link

jlem commented Apr 9, 2014

It would be great if you could pass in an array of objects, and the index key provided merely accessed one of the properties to be used when re-indexing the array of objects.

I know that you can implement the ArrayAccess interface, but it's a bit tedious to do this for every object that you just want to be able to reindex an array of.

A simple check to see if $row is an object would be good. A more robust implementation might allow you to define a method call against a class as the index key e.g. array('Person', 'getID').

Of course this would make it inconsistent with the actual PHP implementation, but it would make this more useful. Having to implement ArrayAccess marginally defeats the purpose of using the terse syntax of array_column() over the slightly more verbose array_map()

@jlem jlem closed this as completed Apr 9, 2014
@jlem jlem reopened this Apr 9, 2014
@ramsey
Copy link
Owner

ramsey commented Apr 9, 2014

I'm trying to ensure that this remains consistent with the PHP implementation. That said, I'll keep this open and see if I can get this proposed and implemented in the core and HHVM, and then I'll add it here. :-)

@vlakoff
Copy link

vlakoff commented Oct 28, 2015

I just noticed PHP 7 will support array of objects :-)

(implemented in php/php-src@3bf012a)

@ramsey
Copy link
Owner

ramsey commented Oct 28, 2015

Interesting. So, if I back-port it into this library, the object support will only work for array_column() in PHP versions 5.4 and earlier, since this library does not override the behavior of the function in 5.5 and 5.6. So, if someone uses this library on 5.4 with objects, when they upgrade to 5.5 or 5.6, their code will stop working.

Thoughts?

@CrandellWS
Copy link

Create a feature override branch that does override and implements newer behaviors there and use that to create a secondary script perhaps array_column_7.

I know a single script is probably prefered but you just can not override and default to core at the same time. I feel it best used as a plug and play type setup(no configs). You have the script it works just choose your preference array_column or array_column_7 ...

@ramsey
Copy link
Owner

ramsey commented Oct 29, 2015

I'd probably namespace it, rather than use array_column_7. Maybe Ramsey\Polyfill\array_column(). I'll give it some thought. Thanks!

@vlakoff
Copy link

vlakoff commented Oct 29, 2015

Personally I had added an object_column() helper in my codebases. This way, arrays and objects are clearly separated, which has pros and cons from an API perspective. At least it has some performance benefit, as the item types don't have to be checked.

Your proposals sound good too, but I can think of some caveats. For example, you could forget to use the namespaced function and it would go unnoticed, the code would work just fine until an object is encountered or you switch from PHP 7 to 5.

@vlakoff
Copy link

vlakoff commented Feb 9, 2016

I would suggest creating a new package "object_column", defining a function object_column() accepting only objects and working with them like array_column() of PHP 7.

Once the user has moved to PHP 7 and doesn't plan to go back, he could simply replace object_column() with array_column() in the codebase to use the native function.

@vlakoff
Copy link

vlakoff commented Feb 9, 2016

And on second thought, -1 on the namespaced function proposal. I think it would be far too prone to forget the import and use the native function by mistake.

@ramsey
Copy link
Owner

ramsey commented Feb 9, 2016

Yeah. I'm not really sure the best approach here. This polyfill is for people using PHP 5.4 and earlier. If anyone is using 5.5 or 5.6, object support in this package won't help them, since they'll be using the native PHP function. I assume people are more likely to go from 5.3 and 5.4 to 5.6 before moving to 7.0, which will cause their applications to break, if they rely on this library with object support.

@vlakoff
Copy link

vlakoff commented Feb 9, 2016

That's precisely why I thought about creating a new package, bringing a separate function object_column which would complement array_column without having to care if it is the native PHP function or the polyfill :)

@shadowhand
Copy link

PHP 7.0 supports objects. Please update this package for those of us stuck on PHP < 7 or HHVM!

@shadowhand
Copy link

Though as it turns out, this won't help us, because PHP/HHVM already defined array_column and so this package will not provide a 7.x compatible function. 😭

@ramsey
Copy link
Owner

ramsey commented Aug 25, 2016

Trying to provide compatibility to previous versions when the function changes drastically across versions in which it is implement is hard.

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

No branches or pull requests

5 participants