Storing objects of custom type in $_SESSION - legal way? I'm trying to figure out: can I load custom class (defined in module) to be loaded before session starts and its contents processed by PHP (unserialized) - is where any legal way to do this? (without hacking Drupal core)

without it I get _PHP_Incomplete_Class if I store something of a custom type to $_SESSION variable.

I did a research:


hook_boot is not called unless page is cached in NO DB mode otherwise it's called after session is loaded
hook_init is called too late (always after session load)


Any thoughts?
actually, I have found a workaround to do so without patching the core.

it is simple.

Drupal 7 core includes all module files which have hook_boot() registred, prior to open session, but hook_boot() is called only then session is started.

What you need to do - is to manually include class declaration outside of a hook_boot() (in a root context of a file containing hook_boot() of your module).

Such code is immediately executed as file being included by Drupal Core (prior to session_start() call), which gives us the desired behavior - we now can store objects of a CustomClass in $_SESSION variable and they wont become corrupted on next page load.

but be carefull if your CustomClass is extending from AnotherCustomClass located in different module and loaded by same mechanics (as described above) - you cannot be sure in order file loads.
This can lead to fatal runtime error: Undefined class