Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69341 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89390 invoked from network); 26 Sep 2013 04:42:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Sep 2013 04:42:05 -0000 Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.44 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.128.44 mail-qe0-f44.google.com Received: from [209.85.128.44] ([209.85.128.44:57533] helo=mail-qe0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/F7-46303-C1BB3425 for ; Thu, 26 Sep 2013 00:42:04 -0400 Received: by mail-qe0-f44.google.com with SMTP id 3so455600qeb.17 for ; Wed, 25 Sep 2013 21:42:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=WJLfmmRVcLaH8LU52oa4SDOPUzjQQqYzlZQSzW0V1Ok=; b=oaumAg6/TVdXtQpHI2WwOOcSfEEY+bDMJwq9woXmbRXI6s7BrlFyEkTQKnFMY76WXg Wn/wMRdhgs7wkoiZMCzHPWTKO/cXO4OGXHV9gwB5+fqHSXJ7tJzeq6uDG38yd8/iMq7f 5+PzBnGRMN6comou2/fbu8GUn0fBvK8O/VjgJWqQqQkCZOFqd0ABEJuxo5L8O8SJwQWW vb+B0j0xmQDTYiiUZtLXdm+0onFHHLsPTp46M4VPqHCxgBODFFIR3Vf+Yi8IbTA6+1Rq f81wkKXvHcj/BBx7Yw1Va0IK3zasdIWz+Gkt0GQPCLEJKVViZxKs3WOZ0C+7sVpiQob6 ykKg== MIME-Version: 1.0 X-Received: by 10.229.244.69 with SMTP id lp5mr49692195qcb.14.1380170521600; Wed, 25 Sep 2013 21:42:01 -0700 (PDT) Received: by 10.49.61.227 with HTTP; Wed, 25 Sep 2013 21:42:01 -0700 (PDT) In-Reply-To: <13848974-B5E7-4044-9F04-C4EB15E09E9C@gmail.com> References: <13848974-B5E7-4044-9F04-C4EB15E09E9C@gmail.com> Date: Thu, 26 Sep 2013 12:42:01 +0800 Message-ID: To: internals Content-Type: multipart/alternative; boundary=001a11c2c54cab6b5c04e741fd66 Subject: Re: [PHP-DEV] PR 287 - added use_keys argument to array_filter() [Discussion] From: tjerk.meesters@gmail.com (Tjerk Meesters) --001a11c2c54cab6b5c04e741fd66 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > > > On 19 Jun, 2013, at 1:04 AM, Patrick Schaaf 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 > --001a11c2c54cab6b5c04e741fd66--