4 ms·
Maybe if it's used solely for debugging, but there are cases where simple functions aren't easy to place. Consider implementing the array_column function for pr
by grobolom 13y ago
Maybe if it's used solely for debugging, but there are cases where simple functions aren't easy to place. Consider implementing the array_column function for pre-5.4 PHP. Or things like method pull, index grouping, or array_select_keys functions.
What helper library should a function like the following exist in?
function array_select_keys(array $dict, array $keys)
{
$result = array();
foreach ($keys as $key) {
if (array_key_exists($key, $dict)) {
$result[$key] = $dict[$key];
}
}
return $result;
}
- deleted 13y ago[deleted]
- kijin 13y agoI have a similar dilemma with some of my helper functions. For example, I have starts_with(), ends_with(), and contains() for super easy string comparisons, is_between() for numerical comparisons, and custom implementations for a few built-in functions such as hex2bin() that don't exist in older versions. These are so general in scope that it would be awkward to place them in their own \Namespace\Class.
- rmrfrmrf 13y agoI'd probably break them all down into classes based on function (EasyString, EasyNumber, BuiltIn). namespace App\Library; class EasyString { public static function starts_with(); ... } and then just use them like this: use App\Library\EasyString as ES; along with an autoloader.
- deleted 13y ago[deleted]
- rmrfrmrf 13y agoUhh, really? array_intersect_key http://php.net/manual/en/function.array-intersect-key.php http://php.net/manual/en/function.array-intersect-key.php
- grobolom 13y agoarray_intersect_key does not do the same as array_select_keys. You can emulate it using array_intersect_key, but either in a more confusing or slow way. $array = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; array_select_keys($array, ['a', 'b']); array_intersect_key($array, ['a' => '', 'b' => '']); // OR array_intersect_key($array, array_flip(['a', 'b'])); // OR Neither are elegant. In either case, that wasn't the point. This is a basic kind of functionality that doesn't belong to any particular class in most code bases. So in this case, having a class of 'helper' functions like these isn't bad.
- rmrfrmrf 13y agoarray_intersect_key([], array_flip([])); is pretty idiomatic actually lol, but that's PHP for you. The point still stands, though, that you shouldn't put array_select_keys into a php file filled with other helper functions. array_select_keys would go perfectly into a helper class called ArrayHelper with a bunch of other array convenience functions. I'm pretty sure the author was talking about a helpers.php file like this: myGetPlaintextUserPasswordFromGET(); encryptPasswordMD5(); //don't use this one anymore connect_to_my_mysql_database(); //complete with hardcoded values increment_for_loop(); my_input_sanitizer(); encryptPasswordSHA1(); //don't use thiS!!! my_improved_input_sanitizer(); //use this one from now on!! add_user_to_database(); getCharacterAtPosition(); generate_a_random_number_between_one_and_ten(); show_user_alert(); encryptPasswordSHA1_withsalt(); //USE THIS OnE! myConvertEmoticonToSmiley(); ... nightmarish.
- grobolom 13y agoThe point still stands, though, that you shouldn't put array_select_keys into a php file filled with other helper functions. It doesn't stand. There's absolutely nothing wrong with having a file with simple helper functions. ArrayHelpers::array_select_keys(); is in no way better than array_select_keys(); There is plenty wrong with the helpers file you described at the end of your post. But it's because those functions break basic programming principles, not the fact that they happen to be simple functions in a file (and most of them are not even simple).