Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5998145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:21:49+00:00 2026-05-23T00:21:49+00:00

I have been using three classes. Two classes extends the third class db. But

  • 0

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

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);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:21:50+00:00Added an answer on May 23, 2026 at 12:21 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Castle MonoRail for the last two years, but in a
I have been using ASP.NET for years, but I can never remember when using
I have been writing several class templates that contain nested iterator classes, for which
We have been using BinarySerialization with our C# app, but the size and complexity
I'm using ASP.NET MVC 3 with Entity Framework CodeFirst I have two classes as
I've been using Liferay a lot for past 2 years, but I have never
I have three classes, one class loads in a configuration file into memory for
I have been using LINQ to SQL for a while, and there is one
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using PHP and JavaScript for building my dad's website. He wants

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.