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 8753491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:26:09+00:00 2026-06-13T13:26:09+00:00

Hello I would like to store or save an object inside a session, using

  • 0

Hello I would like to store or save an object inside a session, using classes like SessionHandler or arrays $_SESSION,I’ve seen that it is possible if I serialize the object, and I don’t want to lose the methods of that object instance.
I’ve seen serializing is possible but the object I want to store is created by PDOStatement::fetchObject() although the instance class is Users I get this error:

PDOException: You cannot serialize or unserialize PDO instances
Why? It is not a PDO instance..

Sorry I am Spanish and I don’t speak English very well..
Thanks

  • 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-06-13T13:26:10+00:00Added an answer on June 13, 2026 at 1:26 pm

    PHP’s native $_SESSION sessions transparently serialize and unserialize objects that support PHP’s serialization protocol or the Serializable interface. You do not need to explicitly serialize them.

    PHP cannot serialize resources because these are handles to some stateful resource outside PHP’s control. This is why you cannot serialize PDO or PDOStatement objects.

    By default an object is serialized by saving all property names and values and unserialized by creating an object with the same class (without invoking the constructor) and directly setting the serialized properties. You can customize serialization behavior for your objects using the __sleep and __wakeup magic methods or by implementing the Serializable interface. But not both! If you use implements Serializable, __sleep and __wakeup are ignored.

    One important note: when using object serialization, you must have the class definition loaded before you unserialize (or have an autoloader that can load it) and it must match the class definition of the object that was serialized. Class definitions are not stored in the serialized data.

    For example suppose you have the following:

    class Test {
        public $version = 1;
        protected $abc;
        public function setAbc($abc) {
            $this->abc = $abc;
        }
    }
    
    $t = new Test();
    $t->setAbc(123);
    $_SESSION['mytest'] = $t;
    

    Now imagine you change Test one day to be like this instead:

    class Test {
        public $version = 2;
        private $def;
        public function setDef ($def) {
            $this->def = $def;
        }
    }
    

    Now suppose you load into your new code an object serialized when Test was at version 1:

    $t = $_SESSION['mytest']; // this was stored yesterday, when Test was version 1
    
    var_dump($t)
    

    You will get this:

    object(Test)#1 (3) {
      ["version"]=>
      int(1)
      ["def":"Test":private]=>
      NULL
      ["abc":protected]=>
      int(123)
    }
    

    Furthermore, you can’t use old methods:

    if ($t->version == 1) { // Check for class version
        $t->setAbc(345); // "Fatal error: Call to undefined method Test::setAbc()"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a datatable that I would like to filter with a single
Hello I have 2 lists that contains the same objects. I would like to
I would like to use entity objects but not store them... I read that
I have some classes in C++ that I would like to expose to Lua.
Hello I would like to display an other splashscreen after the default one, if
hello i would like to know if there is any dll or global hook
Hello I would like to use preg_match in PHP to parse the Desired text
Hello I would like to use the sass module in a play application. We
Hello I would like to know if my script is good; I want to
In the following code I would like to hide Hello world but keep visible

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.