Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:32229 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14547 invoked by uid 1010); 11 Sep 2007 05:01:14 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 14532 invoked from network); 11 Sep 2007 05:01:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Sep 2007 05:01:13 -0000 Authentication-Results: pb1.pair.com header.from=andrew@ashearer.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andrew@ashearer.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain ashearer.com from 208.97.132.83 cause and error) X-PHP-List-Original-Sender: andrew@ashearer.com X-Host-Fingerprint: 208.97.132.83 sd-green-bigip-83.dreamhost.com Linux 2.4/2.6 Received: from [208.97.132.83] ([208.97.132.83:59391] helo=randymail-a3.g.dreamhost.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FA/6C-28088-81126E64 for ; Tue, 11 Sep 2007 01:01:13 -0400 Received: from [192.168.0.6] (pool-70-109-207-38.prov.east.verizon.net [70.109.207.38]) by randymail-a3.g.dreamhost.com (Postfix) with ESMTP id 4D13D185B8D; Mon, 10 Sep 2007 22:00:42 -0700 (PDT) In-Reply-To: <46E5FDE9.1080909@daylessday.org> References: <27DA5A7A-276C-480F-A75C-7F64BB38316B@ashearer.com> <46E5FDE9.1080909@daylessday.org> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-ID: Cc: internals@lists.php.net Content-Transfer-Encoding: 7bit Date: Tue, 11 Sep 2007 01:01:07 -0400 To: Antony Dovgal X-Mailer: Apple Mail (2.752.3) Subject: Re: [PHP-DEV] [PATCH] array_get() From: andrew@ashearer.com (Andrew Shearer) On Sep 10, 2007, at 10:31 PM, Antony Dovgal wrote: > On 11.09.2007 02:12, Andrew Shearer wrote: >> Here's a patch against HEAD that implements the array_get function >> previously suggested on this list. I also attached a test suite, >> which should go in ext/standard/tests/array/array_get.phpt. Feedback >> is welcome. >> >> Independently, someone else had posted the same idea as a feature >> request for PHP 5, and if there's interest I can backport it. >> >> 40792 Open Wish: Function array_get(&$mixed, $key, >> $defaultvalue) >> >> /* Prototype: >> * mixed array_get ( array $search, mixed $key, mixed $default ); >> * Description: >> * Returns the value corresponding to the given key if the key exists >> * in the array. $key can be any value possible for an array index. >> * If the key does not exist, the function returns $default, or FALSE >> * if $default is not specified. Also works on objects. >> * Similar semantics to array_key_exists. >> */ > > Can you explain, what's the difference between your implementation > and this? > > > function array_get(&$array, $key, $default) { > if (isset($array[$key])) { > return $array[$key]; > } > return $default; > } > > ?> The overall idea is to enable cleaner code by having this function available everywhere, including example snippets. This kind of expression is needed all over the place, but is often verbosely written out each time using an error-prone repetition of the array reference. At best a custom function library is included just to get some kind of similar function, which doesn't promote readability or code sharing. There are a few specific technical differences between the function above and array_get. From the proposal: ======= SEMANTICS [...] The semantics match array_key_exists($key, $array) ? $array[$key] : $default ... but for comparison, isset($array[$key]) ? $array[$key] : $default is subtly different. The preferred array_key_exists version has these differences: 1. If $array[$key] exists but has been set to null, that null value will be returned instead of $default. This is likely to be the least surprising thing to do. 2. If $array itself is unset, an error is generated. This is good. The intention is to gracefully handle a missing $key. But if even $array itself doesn't exist, there may be another problem, such as misspelling the array variable. isset() ignores all errors, sweeping more under the rug than we typically want. IMPLEMENTATION A core C implementation of array_get() benchmarked between two and three times as fast as the implementation in PHP. [...] COMPATIBILITY FUNCTION FOR OLDER VERSIONS OF PHP if (!function_exists('array_get')) { function array_get($arr, $key, $default = false) { if (array_key_exists($key, $arr)) { return $arr[$key]; } else { return $default; } } } (This version turned in the fastest times out of several variants. Passing $arr by reference or attempting to return the result by reference had a huge negative impact, and using the ternary ? : operator instead of the if/else was slightly slower.) ======= For the text of the full proposal (which I didn't include in my followups as I slowly came to the realization that attachments were being dropped), see: http://marc.info/?l=php-internals&m=118946242013246&w=2 -- Andrew Shearer http://ashearer.com/