PDO类扩展时对象声明中的错误

PDO类扩展时对象声明中的错误

本文介绍了PDO类扩展时对象声明中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了三个类。一个类是从PDO扩展的db。其他两个类从db类继承。但问题是当我初始化这些子类的对象时,第二个对象被创建为第一个对象的克隆。感谢您提供任何帮助。

I have create three classes. One class is db which extends from PDO. Other two class extends from the db class. But the problem is when I initialize objects of these sub classes the second object is created as clone of the first object. Thanks in advance for any help.

<?php
/** The Database Driver */
define('DB_DRIVER', 'mysql');

/** The name of the database */
define('DB_NAME', 'sample');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', 'root');

/** MySQL hostname */
define('DB_HOST', 'localhost');

class db extends PDO
{
    public function __construct()
    {
        echo "DB constructor called\n";
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        $dsn = DB_DRIVER . ":host=" . DB_HOST . ";dbname=" . DB_NAME;
        parent::__construct($dsn, DB_USER, DB_PASSWORD, $options);
    }
}

class Admin extends db
{
    private $uid, $username, $password, $level, $name, $email;

    public function __construct()
    {
        echo "Admin constructor called\n";
        parent::__construct();
    }
}

class Movie extends db
{
    private $mid, $title, $slug;

    public function __construct()
    {
        echo "Movie constructor called\n";
        parent::__construct();
    }
}


$adminObj = new Admin();
$movieObj = new Movie();
var_dump($adminObj);
var_dump($movieObj);
?>






这是上面的输出。 请注意输出中显示的对象类型


Here is the output of the above. Please note the type of objects shown in the output.

Admin constructor called
DB constructor called
Movie constructor called
DB constructor called
object(Admin)#1 (6) {
  ["uid":"Admin":private]=>
  NULL
  ["username":"Admin":private]=>
  NULL
  ["password":"Admin":private]=>
  NULL
  ["level":"Admin":private]=>
  NULL
  ["name":"Admin":private]=>
  NULL
  ["email":"Admin":private]=>
  NULL
}
object(Admin)#2 (6) {
  ["uid":"Admin":private]=>
  NULL
  ["username":"Admin":private]=>
  NULL
  ["password":"Admin":private]=>
  NULL
  ["level":"Admin":private]=>
  NULL
  ["name":"Admin":private]=>
  NULL
  ["email":"Admin":private]=>
  NULL

}

当父:: __结构($ dsn,DB_USER,DB_PASSWORD,$ options);

When the parent::__construct($dsn, DB_USER, DB_PASSWORD, $options); in the db class is removed the issue will disappear.

推荐答案

您有 PDO :: ATTR_PERSISTENT => ; true,,并且您正在尝试使用相同的DSN字符串创建2个PDO对象。这会告诉PDO在您 $ movieObj = new Movie(); 而不是创建新连接时返回已建立的连接。

You have PDO::ATTR_PERSISTENT => true, and you are attempting to create 2 PDO objects with the same DSN string. This tells PDO to return already established connection when you do $movieObj = new Movie(); instead of creating new one.

您的案例中的解决方案:
PDO :: ATTR_PERSISTENT => false,

Solution in your case:PDO::ATTR_PERSISTENT => false,

有用的链接:

这篇关于PDO类扩展时对象声明中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:25