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 7415301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:16:31+00:00 2026-05-29T07:16:31+00:00

I have been creating PHP projects for years, and have previously written Database classes,

  • 0

I have been creating PHP projects for years, and have previously written Database classes, that are called and created statically, i.e.:

class Database {
    private static $objConnection;

    public static function connect() {
        self::$objConnection = @mysqli_connect(/* Username, password, etc */);

        // Do connection checking etc....
    }
    public static function get_row($strSQL) {/*Do Stuff here*/};
    public static function get_rows($strSQL) {/*Do Stuff here*/};
}

I then in my code can use methods to call: $objResult = Database::get_rows(‘SELECT * FROM User’); for example.

I have the need for multiple databases now (i.e. more than one database for one project), and therefore wanted to extend a base database class, and then extend it for different databases with different settings, so that the 20 or so static calls don’t have to be re-written every time I want to use a new database. So therefore I want to call within my code: MainDatabase::get_rows($strSQL);, and UserDatabase::get_rows($strSQL); for the user database for example. I had read up on LSB previously, and thought this would allow me to do it, but I was stuck with pre 5.30 until now! I went to implement this system using LSB, and ran into problems. Heres the basis of my code (a lot of details removed):

abstract class DatabaseBase {
    abstract protected static function get_db_hostname();
    abstract protected static function get_db_user();
    abstract protected static function get_db_password();
    abstract protected static function get_db_database();

    /**
     * Connects to the database
     * Will retur true or false, depending on if it could connect or nots
     *
     * @return boolean
     */
    final private static function connect() {
        $strHost        = static::get_db_hostname();
        $strUser        = static::get_db_user();
        $strPassword    = static::get_db_password();
        $strDatabase    = static::get_db_database();
        static::$objConnection = @mysqli_connect(/*Users, pass, etc*/);
    }

    final public function run_query($strSQL) {
        /* Check and parse the SQL, etc, etc*/
        return(static::$objConnection->query($strSQL));
    }
}

class MainDatabase extends DatabaseClass {
    protected static $objConnection;
    protected static function get_db_hostname() {return('localhost')};
    /* Etc etc*/
}   

class UserDatabase extends DatabaseClass {
    protected static $objConnection;
    protected static function get_db_hostname() {return('user_db_host')};
    /* Etc etc*/
}

This works if called and tested from index.php for example not IN a class. The problem is when I call MainDatabase, and UserDatabase::run_query from within another class. Such as the following:

class User {
    public function load($idUser) {
        $objRow = UserDatabase::run_query('SELECT * FROM User WHERE idUser = $idUser');
    }
}

If I call the load method on the User class, then I get the error: “Fatal error: Access to undeclared static property: User::$objConnection in DatabaseBase.php on line xx”

If I output the results of get_called_class() in the run_query() method it also says User.

User does NOT extend database in anyway, and is NOT static. Maybe this is my understanding of LSB, but can someone help. My DatabaseBase class will define lots of static methods that can be used on database connections, and the child classes will just contain the static connection object, and connecting/settings information. I am getting a little disappointed with this, as I have been waiting 6 months to try out LSB as a solution to this, and found that this is not what I wanted.

Regards, Gunja

  • 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-29T07:16:32+00:00Added an answer on May 29, 2026 at 7:16 am

    If you run UserDatabase::connect() it should store the objConnection from

    static::$objConnection = @mysqli_connect(/*Users, pass, etc*/);
    

    indeed in the UserDatabase class (and not in the DatabaseBase class). Then,

    UserDatabase::run_query('SELECT * FROM User WHERE idUser = $idUser');
    

    should work, but you might want to make run_query static as well!

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

Sidebar

Related Questions

I have been reading that creating dependencies by using static classes/singletons in code, is
I have been creating a helper class for the Facebook PHP API in order
I have been creating a php application that makes quite a few queries to
I have been creating Unit tests like crazy and find that I'm often having
I have been creating a peer to peer connection for a new game, that
I have been tasked with creating a program that will generate an amortization schedule.
Hello I have been tasked with creating a fairly complex web application in php,
I have been creating a web scraper for an internal application with PHP but
I have been working on creating an application that sends a string from an
I have been trying to override the database method of the loader class (CI_Loader).

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.