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 8296759

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:08:37+00:00 2026-06-08T15:08:37+00:00

I have a class Database.php, which is an abstract Singleton class: <?php abstract class

  • 0

I have a class Database.php, which is an abstract Singleton class:

<?php

abstract class Database
{
    protected static $_instance;
    ...

    public static function instance()
    {
        $class = get_called_class();

        if (!self::$_instance) {
            self::$_instance = new $class();
        }

        return self::$_instance;
    }
}

?>

Other databases extend this class and implement the abstract functions, so that I can change the database I use, while making sure my application still works as long as it uses the functions in this abstraction layer (I have left out the function definitions from the above code).

I have a class PDO, in PDO.php. It extends database and also includes the instance() function identical to the one above, but it does not have it’s own $_instance variable.

class PDO extends Database
{
    //...

    public static function instance()
    {
        $class = get_called_class();

        if (!self::$_instance) {
                self::$_instance = new $class();
        }

        return self::$_instance;
    }
}

Originally I thought I wouldn’t have to include the instance() function in the PDO class, since it inherits from Database. But I included it because I was getting this error:

Fatal error: Call to undefined method PDO::instance()

The problem is that I’m still getting this error, even with the code above. I don’t know if this is related, but there is another strange error I was getting. The type of database I want to use, in this case PDO, is stored in a variable accessible through App::setting('DB'). The Model base class in my MVC framework loads the appropriate database class and stores it in $this->_db. Here is the code for the Model:

<?php

require_once 'Databases/Database.php';

class Model
{
    protected $_db;

    public function __construct()
    {
        $database = App::setting('DB'); // 'PDO' 
        require_once 'Databases/' . $database . '.php';
        $this->_db = $database::instance(); // this is what triggers the error
    }
}

?>

Now this code was giving me an error that I could not redeclare the PDO class. I searched online, and found that the problem is usually related to using include() instead of require_once(). I checked everywhere and my Autoloader, didn’t see any include(). Even with require_once() in the model above, it was still giving me the error (this is the only place I was requiring the PDO class…).

To fix that error, I am using a band-aid solution where I replace the require in the Model constructor with:

if (!class_exists($database)) {
    require_once 'Databases/' . $database . '.php';
}

Can anyone explain what is going on here?

  • 0 0 Answers
  • 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-06-08T15:08:40+00:00Added an answer on June 8, 2026 at 3:08 pm

    The class name PDO is a standard class of PHP, so with your solution to include your PDO class you are actually never including yours.

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

Sidebar

Related Questions

In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have a database class, which an instance is declared in the main index.php
So I have this database class in PHP and I only have 1 function
I have created a class file database.php which handles all the sql queries and
i have these in Database.php class Database { private $dbname = 'auth'; private $db_instance;
at my working place (php only) we have a base class for database abstraction.
I have a class which defines a historical extraction on a database: class ExtractionConfiguration
I have a database class, which manages all my mysql connections for my application.
I have a class with like 20 fields which get populated from SQL database
I have an abstract parent class Item , from which different types of items

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.