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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:13:45+00:00 2026-06-05T09:13:45+00:00

I need to pass a PDO connection into a cart class from a controller

  • 0

I need to pass a PDO connection into a cart class from a controller,

function __construct($connection) 
{
    $this->cart = new cart($connection);
}

but I think the problem is with serialize()

public function render_page() 
{

    if (!isset($_SESSION[SESSION_CART]))
    {
       $cart =  $this->cart;
    }
    else 
    {
       $cart = unserialize($_SESSION[SESSION_CART]);
    }

    $_SESSION[SESSION_CART] = serialize($cart); 

 }

I get this error,

Fatal error: Uncaught exception ‘PDOException’ with message ‘You
cannot serialize or unserialize PDO instances’ in
C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php:89
Stack trace: #0 [internal function]: PDO->__sleep() #1
C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php(89):
serialize(Object(cart)) #2
C:\wamp\www\store_2012_MVC\local\controllers\class_factory.php(75):
base_extended_cart->render_page() #3
C:\wamp\www\store_2012_MVC\index.php(69): factory->render() #4 {main}
thrown in
C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php
on line 89

How can I fix this?

Or can I use something else instead of serialize()?

EDIT:

I tried it with __sleep and __wakeup magic methods but still get the same error,

class database_pdo
{
    # database handler
    protected $connection = null;

    # make a connection
    public function __construct($dsn,$username,$password)
    {
        try 
        {

            $this->connection = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

        }
        catch (PDOException $e) 
        {
            # call the get_error function
            $this->get_error($e);
        }
    }

    # don't forget to add getter method to get $this->connection, it's just a good practice.
    public function get_connection()
    {
        return $this->connection;
    }

    public function __sleep()
    {
        return array('connection');
    }

    public function __wakeup()
    {
        $this->connection;
    }

}
  • 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-05T09:13:46+00:00Added an answer on June 5, 2026 at 9:13 am

    Take a look at the __sleep and __wakeup magic methods.
    http://us.php.net/manual/en/language.oop5.magic.php#object.sleep

    They allow you to specify which properties get serialized and which get ignored. The issue there is that you’ll need to regularly pass in an instance of your PDO object.

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

Sidebar

Related Questions

I need to pass a View's class name as CommandParameter. How to do this?
I need to pass a value from PHP to C++. I think I can
I need to pass a lot of variables to a PHP class from a
I need to pass id to another controller action in the controller. I try
I need to pass a dictionary from a python script to a Django app.
i need to pass an array from my zend action to the view, possibly
I need to pass audio data into a 3rd party system as a 16bit
I'm working with the DeviceIoControl function and I need pass the IOCTL_ATA_PASS_THROUGH value to
In order to refactor some methods into smaller ones, I need pass by reference,
I want add a logger function to a worker class, how to pass a

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.