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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:14:16+00:00 2026-06-14T03:14:16+00:00

I am wondering if my understanding is right. I placed my database connection parameter

  • 0

I am wondering if my understanding is right.

I placed my database connection parameter on my __construct(), so this means every time the class is instantiated, I also reconnect to my database, right?

private $handle;
public function __construct()
{
  $username = "test";
  $password = "9712nc*<?12";
  $host = "localhost";
  $db = "miner";
  $dsn = 'mysql:dbname='.$db.';host='.$host;
  try {
    $this->handle = new PDO($dsn,$username,$password);
  }
  catch(PDOException $e) {
    print $e->getMessage();
    die();
  }
}

Is this a good practice if I have many request from a certain user? Does this mean every time a user make a request (even if the request is just done minutes ago) the script should first connect to the database? Is there any appropriate way I can preserve my $handle?

BTW: The database connection is working fine.

  • 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-06-14T03:14:17+00:00Added an answer on June 14, 2026 at 3:14 am

    If the class is instantiated once, then you’re going to be just fine. In this case, you open one connection only.

    If you instantiate the class several times, or use it as a static class, then you’re potentially going to create a connection each time, which is not ideal.

    If you keep the classes all active (i.e. you never delete reference to a class once you create it, from memory (I’ve never tested it) the internals of PHP should actually sort this out for you, and you’ll still only have the one connection to the database. But if you lost a handle to the class you created, then the PDO will be destroyed and you start again. If you are using as a static class (i.e. you call it with class:function()), then you’re on the wrong path.


    There are two common solutions, and you’ll find arguements for and against both of them.

    1) Use a global for storing your DB connection. You create the $handle = PDO and store $handle as a global variable. Easy to pass around. Easy to overwrite – I’m not going to debate here.

    2) Create a “static” class (commonly called a Singleton) that you recall. The basic structure would be

    private static $ThisObj;
    private static $handle;
    
    public static function getInstance() {
        if( !(self::$ThisObj instanceof SoapDB) ) {
            self::$ThisObj = new SoapDB();
            try {
                $this->handle = new PDO_Handler('mysql:dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASS);
                $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                // Error Here
            }
        }
        return self::$ThisObj;
    }
    

    Using this, you would normally create a separate DB class as a singleton. Using Singleton vs Global: your call (after research).

    3) The third answer is if you are only ever calling your class above as a static (i.e. you call class::function()) then you can just make the $handle as a static and actually your problems should be wonderously solved. Essentially you’re creating a Singleton, with the database as one of the properties.

    Sorry that there’s no wrong or right answer.


    Edit based on comment “What’s a static class”:

    Strictly speaking, I should say “class with static properties”. To explain, normally you create a class with the syntax:

    $myClass = new class();
    $methodResult = $myClass->method();
    

    and then do lots of nifty little things with $myClass by calling functions. You can then drop reference to the class, lose the properties (variables) and will reinitilaise later on.

    A static class (for the purposes of this answer) is one where you have static properties. Normally (not absolutely) they are called by going.

    $methodResult = class::method();
    

    You don’t hold on to the class – it gets initiated, used and dropped. However, if you store properties (vars) as static then each time you use that class, those properties will still exist in the state they last were. So method can set $this->MyVar = 'something' and next time, $this->MyVar will still be something.

    Very useful for compartmentalising your code, stops functions overwriting others and can make unit testing easier.

    (Note – I’m generalising to make it simpler. But it should give you an idea of the processes.)

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

Sidebar

Related Questions

I was wondering can node.js run with Apache server? My understanding with this language
I was wondering if someone could help me understanding fixing my time step within
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
I'm slowly understanding closures more and more, and the following code works. I'm wondering
Wondering what the best / good way of doing this would be in jQuery.
Wondering what is the best or most popular database client tool. Similar to Microsoft's
I'm wondering if anyone with a better understanding of python and gae can help
I am not sure if I am understanding the google documentation, I am wondering
I was wondering what it means to set allowStale = true on set operations
i really have a hard time understanding the SetThreadAffinityMask function. Im trying to implement

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.