by bru · 13y ago · 0 comments
In debugging.php alongside with other debugging helpers?
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;
} 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. array_intersect_key
http://php.net/manual/en/function.array-intersect-key.php $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.
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.