使用继承时对象声明中的错误

时间:2011-06-10 12:47:41

标签: php oop class constructor

我一直在使用三个班级。两个类扩展了第三类db。但问题是当我声明这些类的对象时,第二个对象被创建为第一个对象的克隆。提前感谢您的帮助。

        class db extends PDO {
    public function __construct() {
            echo "DB constructor called\n";
            ..
    }
        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();
}

$base_path = "../../";

require $base_path . 'config.php';
require $base_path . 'lib/class.db.php';
require $base_path . 'lib/Admin.php';
require $base_path . 'lib/Movie.php';

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

输出

Admin constructor called
DB constructor called
Movie constructor called
DB constructor called
object(Admin)#1 (11) {
  ["uid":"Admin":private]=>
  NULL
  ["username":"Admin":private]=>
  NULL
  ["password":"Admin":private]=>
  NULL
  ["level":"Admin":private]=>
  NULL
  ["name":"Admin":private]=>
  NULL
  ["email":"Admin":private]=>
  NULL
  ["error":"db":private]=>
  NULL
  ["sql":"db":private]=>
  NULL
  ["bind":"db":private]=>
  NULL
  ["errorCallbackFunction":"db":private]=>
  NULL
  ["errorMsgFormat":"db":private]=>
  NULL
}
object(Admin)#2 (11) {
  ["uid":"Admin":private]=>
  NULL
  ["username":"Admin":private]=>
  NULL
  ["password":"Admin":private]=>
  NULL
  ["level":"Admin":private]=>
  NULL
  ["name":"Admin":private]=>
  NULL
  ["email":"Admin":private]=>
  NULL
  ["error":"db":private]=>
  NULL
  ["sql":"db":private]=>
  NULL
  ["bind":"db":private]=>
  NULL
  ["errorCallbackFunction":"db":private]=>
  NULL
  ["errorMsgFormat":"db":private]=>
  NULL
}

修改后的代码,请分析一下。当父:: __构造时($ dsn,DB_USER,DB_PASSWORD,$ options);在删除db类中,问题将消失。

<?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);
?>

3 个答案:

答案 0 :(得分:0)

他们不是克隆人。它们都是从db类继承的,并且您没有对它们进行任何更改。它们不是克隆 - 它们完全相同。

答案 1 :(得分:0)

如果要阻止2个PDO实例,请使用Singleton pattern

答案 2 :(得分:0)

我看不出任何问题 - 输出完全符合预期。因此,您所包含的文件中的其他位置必定是错误。以下是独立测试代码:

<?php
class db extends PDO
{
    public function __construct()
    {
        echo "DB constructor called\n";
    }
}

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);

这是输出:

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(Movie)#2 (3) {
  ["mid":"Movie":private]=>
  NULL
  ["title":"Movie":private]=>
  NULL
  ["slug":"Movie":private]=>
  NULL
}

所以请检查您的实际代码。也许(只是可能)你包含了错误的文件..或者第二类在另一个地方被宣告(你可能正在编辑错误的文件)......

修改 亚历克斯,谢谢你提供更多信息。我在更新后的代码中遇到了问题:PDO::ATTR_PERSISTENT => true, - 将true更改为false并查看差异:)