I've been getting into PHP namespaces and had a thought about how to clean things up a bit in my custom modules. I was thinking that for hooks I could use the traditional Drupal method of defining functions in the global namespace with the module name prefix but for all other functions and classes I could define them inside a PHP namespace. So, for my module "donut_win" a file would look like this:
<?php
namespace {
function donut_win_menu()
{
$items['donut/win/%'] = array(
'title' => 'Hey, there!'
,'description' => "Eveyone wins when donut wins."
,'page callback' => 'Donut\\Win\\render_page'
,'page arguments' => array(1)
,'access callback' => true
,'type' => MENU_CALLBACK
);
return $items;
}
function donut_win_cron()
{
# magic!
}
} # namespace global
namespace Donut\Win {
function render_page ($bingo)
{
# so much magic!
}
}
I'm interested in what potential pitfalls would be with this method and any other disadvantages or advantages. Is this a good idea or am I crazy?