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?
<?php
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/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php