Error in object declaration when inheritance is used
I have been using three classes. Two classes extends the third class db. But the problem is when I declare objects of these classes the second object is created as clone of the first object. Thanks in advance for any help.
class db extends PDO {
public function __construct() {
echo "DB constructor called\n";
..
}
class Admin extends db {
private $uid, $username, $password, $level, $name, $email;
public func开发者_JAVA百科tion __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);
Output is
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
}
Modified code, please analyse this. When the parent::__construct($dsn, DB_USER, DB_PASSWORD, $options); in the db class is removed the issue will disappear.
<?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);
?>
I see no problems at all -- the output is exactly as expected. So it must be an error somewhere else in the files you are including. Here is the standalone test code:
<?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);
And this is 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(Movie)#2 (3) {
["mid":"Movie":private]=>
NULL
["title":"Movie":private]=>
NULL
["slug":"Movie":private]=>
NULL
}
So please check your actual code. Maybe (just maybe) you are including the wrong file .. or the 2nd class got declared in another place (and you may be editing wrong file)...
EDIT:
Alex, thank you for providing more info. I have ound the problem in your updated code: PDO::ATTR_PERSISTENT => true
, -- change true
to false
and see the difference :)
They are not clones. They are both inheriting from the db class, and you haven't done any changes to them. They are not clones - they are simply identical.
If you want to prevent 2 PDO instances, use Singleton pattern
精彩评论