Comments about CRM_Utils_Array::value when used as a replacement for empty/isset
-
Its name isn't very descriptive of what it does and I find sometimes makes code LESS readable. A better name might be something like getElementByKeyOrDefault. And the order of parameters is backwards from the typical array element syntax: value($b, $a) vs
a[
b]. -
It has the potential to create some strange bugs. For example what would someone who's maybe copying and pasting code from different spots expect the final output of this code to be? They'd intuitively be wrong.
$contact = <some CRM_Contact_BAO_Contact object from somewhere>;
/* Let's say it looks something like:
var_dump($contact);
class CRM_Contact_BAO_Contact#518 (76) {
public $id =>
string(3) "100"
public $contact_type =>
string(10) "Individual"
public $sort_name =>
string(13) "Smith, John"
public $display_name =>
string(12) "John Smith"
... etc ...
}
*/
if (CRM_Utils_Array::value('contact_type', $contact) == 'Individual') {
echo "It's an individual";
} else {
echo "Not an individual";
}
The answer is it outputs "Not an individual", even though the contact_type member is "Individual";
- A very very very minor thing, but CRM_Utils_Array::value is slower than just using ?: with isset() or empty(). My own benchmarking a loop of 10000 iterations gives an average 500 milliseconds vs 200 milliseconds. It's tiny and inconsequential, but just for the record it is consistently slower.