Re: PR 287 - added use_keys argument to array_filter() [Discussion]

From: Date: Thu, 26 Sep 2013 04:42:01 +0000
Subject: Re: PR 287 - added use_keys argument to array_filter() [Discussion]
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi,

I've updated my patch to allow a range of values to be passed as the third
argument: pass key, pass value or pass both.

Instead of using OR-able constants, I went with an enumeration type because
there are only going to be three options; it just felt wrong to have
ARRAY_FILTER_USE_KEY | ARRAY_FILTER_USE_VALUE instead of a simpler
ARRAY_FILTER_USE_BOTH or even just true.

Updated examples can be found here: https://github.com/php/php-src/pull/287

Let me know if there are any major objections to the patch.



On Wed, Jun 19, 2013 at 7:15 AM, Tjerk Meesters <[email protected]>wrote:

>
>
> On 19 Jun, 2013, at 1:04 AM, Patrick Schaaf <[email protected]> wrote:
>
>
> > var_dump(array_filter(['foo', '', 'bar'],
> > 'strlen', true));
> >
> > Warning: strlen() expects exactly 1 parameter, 2 given in - on line 1
> >
> > Not only do we trigger the error handler (a large enough array causes a
> > performance issue), but we also get back an empty array as a result.
> That's
> > BC and performance loss that's simply unacceptable, so defaulting the
> > behavior to pass the key is simply out of the question.
>
> Existing code won't pass that third, true, argument to array_filter(), and
> thus will not break.
>
> Nevertheless, I'd say go with a new array_filter_key() function, because
> that permits using existing one-parameter functions like that strlen as a
> callback, instead of forcing two-parameter callback functions.
>
> I've mirrored the behaviour of this feature from JavaScript as much as
> possible (but obviously without passing the array itself as the third
> argument). As such I don't feel there's a need to introduce yet another
> function just so that the key is passed as the first argument. And to use
> both key and value forces the developer to use closures instead of just any
> callback unless this means the second argument passed to the callback is
> the array :)
>
> Another alternative might be to go with a third argument to
> array_filter(), but make that an integer with ORable constants
> ARRAY_FILTER_KEY, ARRAY_FILTER_VAL - only one of them set calls with a
> single argument, both set call with two arguments.
>
> I see some potential here, albeit a bit more magical :) Let's see how the
> discussion progresses.
>
> best regards
>   Patrick
>
> --
> --
> Tjerk
>


Thread (39 messages)

« previous php.internals (#69341) next »