Explosion Pills
Explosion Pills

Reputation: 191779

Dependency Injection and serialized classes

I'll just get right into it. I have a class User that needs to do some DB operations (using the class DB). The controllers create DB as appropriate, and inject it into User with its constructor.

When a user is logged in, a User object is stored to the session. The problem is that DB cannot be serialized to the session, so when User wakes up, its db member is null, which is bad. I solved this pretty simply with

public function __wakeup() { $this->db = new DB; }

..however, this is definitely a violation of DI, and it could even cause problems down the line if the DB needs to be different depending on the controller (the controller creates the DB it needs, after all).

Problem is that when User is unserialized from the session, it's not constructed again, so it doesn't have a chance to get the DB member. I have several possible solutions, and the problems each has:

Any suggestions?

Upvotes: 3

Views: 1445

Answers (1)

hakre
hakre

Reputation: 198115

As your user object has a dependency of the DB object and the DB object can not be serialized, your user object itself can't be serialized either.

So basically your problem is that you serialize an object that is not serialize-able.

What you actually need is session state. Create a session state object that is able to pick models (set) and which is able to provide models (get).

By adding the logic to the session object how a specific object can be serialized (or better: stored in session, e.g. you normally only need to store the ID if it's a database model) this will work like any other factory.

Then inject the Session object as a dependency.

Upvotes: 3

Related Questions