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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:22:13+00:00 2026-05-27T06:22:13+00:00

I would like to know if creating a class like the one below would

  • 0

I would like to know if creating a class like the one below would be a good practice.

So I would use the self::$mysqli object every time I need to do some work and the connection would only get closed on __desctruct(). Plus, extending the class would still maintain only one connection since $mysqli is declared static.

this way I would have only one connection thought the script

class db {
 protected static $mysqli;

 function __contruct(){
  $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
  // DB_HOST, DB_USER, DB_PASS, DB_DATABASE defined elsewhere
 }

 function __destruct(){
  $this->mysqli->close();
 }
}

I would like to know your insights about this.

  • 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-27T06:22:13+00:00Added an answer on May 27, 2026 at 6:22 am

    I would like to know if creating a class like the one below would be a good practice.

    I would consider it not to be good practice. That’s especially for one reason: the problem you describe why you think you need it: Only one connection per request.

    You don’t need to code anything. mysqli has a nice feature build in, the default connection. It picks the database connection parameters from the ini settings (configuration) each time you create a new instance:

    $db = new mysqli;
    

    As all database connections get closed when the script finishes, there is not much to care about at all. The only thing you need to do is to instantiate the connection once and pass the variable around to all code that needs that mysqli object.

    Another bad practice is that you’re introducing a Singleton Doc here, something like:

    class MysqliSingleton extends mysqli
    {
        private static $instance;
        /**
         * @return MysqliSingleton
         */
        public function getInstance()
        {
            if (NULL === self::$instance)
                self::$instance = new self();
            return self::$instance;
        }
        private function __construct()
        {
        }
        public function __clone()
        {
             throw new RuntimeException('Clone is not allowed.');
        }
        public function __wakeup()
        {
            throw new RuntimeException('Unserializing is not allowed.');
        }
    }
    

    It would work like this:

    $mysqli = MysqliSingleton::getInstance();
    

    and would always return that one mysqli instance, which might be what you’re looking for. However Singletons are considered harmful as they can introduce a lot of problems see the related question Who needs singletons?.

    It’s easier you create yourself a variable that you pass around containing the database instance. You can additionally encapsulate some sort of lazy-loading in case your application does not always need a mysqli connection, e.g. with a very simple context class:

    interface ContextMysqli
    {
        /**
         * @return mysqli
         */
        public function getMysqli();
    }
    
    class Context implements ContextMysqli
    {
        private $mysqli;
        public function getMysqli()
        {
            $this->mysqli || $this->mysqli = new mysqli();
            return $this->mysqli;
        }
    }
    

    When your scripts start, just instantiate your context and pass it to every part of your code where you need it:

    $context = new Context();
    ...
    $result = do_some_db_work($context);
    ...
    function do_some_db_work(ContextMysqli $context)
    {
        $mysqli = $context->getMysqli();
        ...
    }
    

    This suggestion might be a little shortsighted as well, but it’s better than a singleton while not introducing much ado. The benefit is that you have encapsulated the logic when to create the mysqli object w/o the need of a singleton. Your code is now independent to global or static context.

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

Sidebar

Related Questions

I would like to know if there are general rules for creating an index
I am creating a lightweight editor in C# and would like to know the
I am creating an iphone performance app in which I would like to know
Would like to know what a programmer should know to become a good at
I would like to know what kind of tool you use for writing your
i would like know some reference. I know i can googling it. but prefer
Would like to know the c# code to actually retrieve the IP type: Static
I would like to know if I can open 2 different diagrams using MS
I would like to know which dependency described in my pom.xml brings a transitive
I would like to know what semaphores, messageQueues, etc... are active in my vxWorks

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.