hook_init() are invoked from _drupal_bootstrap_full(), which contains the following code.
menu_set_custom_theme(); // It invokes hook_custom_theme().
The reason of the order is explained in the comment before that code, which says:
Prior to invoking
hook_init(), initialize the theme (potentially a custom one for this page), so that:
- Modules with
hook_init() implementations that call
theme_get_registry() don't initialize the incorrect theme.
- The theme can have
hook_*_alter() implementations affect page building (e.g.,
hook_page_alter()), ahead of when rendering starts.
It seems that your situation is a circular-reference situation, where an implementation of
hook_custom_theme() depends from
hook_init(), and an implementation of
hook_init() could depend from the value returned from
If you are sure that the result of
hook_custom_theme() doesn't influence the code of
spaces_init(), which doesn't need the theme to be initialized, then you could invoke
spaces_init() in your
module_invoke('spaces', 'init'), but that sounds like a hack, and subject of problems if
spaces_init() is changed, and it actually needs a theme to be initialized before being executed.