Where to write public, common, utility functions in Drupal? At times you have to write little utility functions which will have a general scope (not restricted to the module you are working on) so that it can be called from anywhere in the project.

I know I can write it anywhere and include the php file from the caller, but is there a convention followed in Drupal?

For e.g. I am working on xyz.module which does xyz thing which will have a silly

function startsWith($haystack, $needle) // case-insensitive startsWith
{
    return strncasecmp($haystack, $needle, strlen($needle)) === 0;
}


Where shall I write that function?
As many API modules do, you can place your global helper functions within a dedicated module. (I'll be using MYHELPERMODULE in the following example.)

You then should follow the naming conventions:

function MYHELPERMODULE_startswith($needle, $haystack) {
  // do your magic
}


You can call this function from within all other modules, as long as your helper module is active. To assure it's active, add your helper module as dependency to the .info file of the module that is using the helper function:

dependencies[] = MYHELPERMODULE