0

I have these classes below for my online store.

This super class holds all common methods used by the child classes.

class grandpa
{
    public function test1($string)
    {
        return $string;
    }
}

So do the PDO connection,

class database_pdo extends grandpa
{
    protected $connection = null;
    protected $dsn,$username,$password;

    public function __construct($dsn,$username,$password)
    {
        $this->dsn = $dsn;
        $this->username = $username;
        $this->password = $password;
        $this->get_connection();
    }

    public function get_connection()
    {
        try
        {
            $this->connection = new PDO($this->dsn, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        }
        catch (PDOException $e) 
        {
            $this->get_error($e);
        }
    }

    public function __sleep()
    {
        return array('dsn', 'username', 'password');
    }


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

    public function get_error($e) 
    {
        $this->connection = null;
        die($e->getMessage());
    }

    public function __destruct()
    {
       $this->connection = null;
    }
}

I have this class extend from pdo for other common methods that require pdo connection,

class papa extends database_pdo
{
    protected $connection = null;

    public function __construct($connection)
    {
        $this->connection = $connection;

    }

    public function test2($string)
    {
        return $string;
    }

}

The child classes,

class kido_1 extends papa
{

    public function __construct($connection)
    {
        parent::__construct($connection);
    }

    public function test3($string)
    { 
        return $string;
    }
}

How it use the classes above,

# Host used to access DB.
define('DB_HOST', 'localhost');

# Username used to access DB.
define('DB_USER', 'xxx');

# Password for the username.
define('DB_PASS', 'xxx');

# Name of your databse.
define('DB_NAME', 'xxx'); 

# Data source name.
define('DSN', 'mysql:host='.DB_HOST.';dbname='.DB_NAME);

$connection = new database_pdo(DSN,DB_USER,DB_PASS);

$kido = new kido($connection);

$_SESSION['cart'] = serialize($kido);

$kido = unserialize($_SESSION['cart']);

print_r($kido->test3('hello'));

I get this error,

invalid data source name

It is caused by unserialize() that I need it for my cart data...

How can I fix this? Or a better way of rewrite these classes?

Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

1

Your papa::connection is a PDO object. Therefore, when you are trying to serialize $kido, you are trying to serialize a resource, which is not possible. Try adding a $this->connection = null; in your database_pdo::__sleep() method.

Community
  • 1
  • 1
RandomSeed
  • 29,301
  • 6
  • 52
  • 87
  • sorry I'm not quite get it - `__sleep()` method is in the class of `database_pdo` but not `grandpa` - do you mean `papa`? – Run Jun 19 '12 at 00:37
  • Also removed "[PDO object] initialized at construction-time", I indeed mixed up `grandpa` and `papa` – RandomSeed Jun 19 '12 at 00:43
  • 1
    Darn, also, `sleep()` is a method of `database_pdo` class. I seriously got confused ;) I guess it's time to go to sleep. – RandomSeed Jun 19 '12 at 00:46
  • I think I should extend `grandpa` not `database_pdo`. see my answer below which now works fine. thanks. – Run Jun 19 '12 at 00:48
0

a solution I think...

class papa extends grandpa
{
    protected $connection = null;

    public function __construct($connection)
    {
        $this->connection = $connection;

    }

    public function test2($string)
    {
        return $string;
    }

}
Run
  • 54,938
  • 169
  • 450
  • 748