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?

4

2 回答 2

1

papa::connection是一个 PDO 对象。因此,当您尝试序列化时$kido,您正在尝试序列化资源,这是不可能的。尝试$this->connection = null;在您的database_pdo::__sleep()方法中添加一个。

于 2012-06-18T23:46:35.687 回答
0

我认为的解决方案...

class papa extends grandpa
{
    protected $connection = null;

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

    }

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

}
于 2012-06-19T00:47:01.613 回答