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?

  • What's 'NO DB mode'? hook_boot() will be called on every single request that goes through Drupal (regardless of caching status), so I'm guessing you have contrib modules installed that change that behaviour, or some core patches or something?
    – Clive
    Commented Jul 30, 2013 at 12:56
  • I mean what if You look into Drupal Core sources You'll see that bootstrap_invoke_all($hook) called 2 times one in PAGE_CACHE phase of drupal bootstrap and one in PAGE_HEADER (which is after session start) The one which called before session start is not invoked unless there is a cached version of requested page stored not in Database.that's what i meant. Commented Jul 30, 2013 at 13:17
  • no core patching. this is that I'm trying to avoid by asking this question. Commented Jul 30, 2013 at 13:19
  • OK I think I get you, but just to confirm - you're serializing a custom class into a session variable, and at some point when you read that variable back in and unserialize(), PHP complains about an incomplete class?
    – Clive
    Commented Jul 30, 2013 at 13:20
  • 1
    sorry, it's under NDA. This is not comes with a concrete class. (anyway mine contains only usual methods, no magic) problem is that I cant load class definition in memory before drupal_session_start() occurs and it leads to this error. There are many possible solutions of my problem, e.g: 1)not to use $_SESSION - store it somethere separately and mantain load process by myself. 2)double serialize it into the session and mantain unserialization by myself. The answer I'm trying to get: is there a way to rely on standart biuld in PHP mechanics of storing session across page requests or not Commented Jul 30, 2013 at 13:53

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.