Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:48:18+00:00 2026-05-28T04:48:18+00:00

I’ll just get right into it. I have a class User that needs to

  • 0

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:

  • Allow DB to be set by setter injection
    • This also doesn’t seem like a great solution. It also opens up the possibility of setting the DB at an inappropriate time, and still seems to violate the DI spirit. The controller also has to know to set the DB
  • Don’t serialize the object, but serialize a token and recreate the object every time
    • This would be inconvenient and seems inefficient. This could also lead to some repetition (each controller would need to do something like $usr = new User($_SESSION['user-token'], new DB);). On the other hand, keeping the User object out of superglobals would dissuade some nasty global variable usage.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:48:19+00:00Added an answer on May 28, 2026 at 4:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.