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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:18:14+00:00 2026-05-27T08:18:14+00:00

I have two PHP classes. One is for connecting to the database, building queries,

  • 0

I have two PHP classes. One is for connecting to the database, building queries, executing them, and disconnecting from the database. The other class is for users: adding them, updating them, logging them in, etc.

I’m debating whether I should connect to the database on the page globally and use that connection (passing the database object into the method of the user object), or whether I should connect and disconnect from the database from within the a user method itself.

The advantage I see for connecting globally, is that once connected I have that connection available for executing multiple methods. The disadvantage is that I 7need to worry about passing the database object around.

The advantage of connecting within the method is that it’s completely transparent, however, there might be 4 or 5 connections established and disconnected, which may lead to an overhead.

Is there a best practice for using either, or does it really depend on the amount of users and the server’s specs e.g. memory, cpu, etc. The system needs to support up to about 1,000 users, so it’s fairly small scale.

Any feedback would be much appreciated.

-Ryan

  • 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-27T08:18:14+00:00Added an answer on May 27, 2026 at 8:18 am

    Create a function that creates the connection on demand and provides access to the connection object:

    function getDb()
    {
        static $db;
        if (!$db) $db = ...;
        return $db;
    }
    

    Wrapped into class this approach is called Singleton

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

Sidebar

Related Questions

I have two classes, one of them is a configuration class. Inside of this
I have two classes in a PHP application, B extending A for example. Class
Here's the objective. I have a PHP class and there are one or two
I have two class. First class = database.class.php (sub)->vericek.class.php Database.class.php is: Class Database {
I am confused with & and && . I have two PHP books. One
I have two classes that work seperate from another, but they extend the same
For example, I have a directory structure like this: my_stuff classes one two more
I have two classes: News : /** @Entity @Table(name=news) */ class News { /**
I have this code (two classes) class url { private $profile_id; private $short; public
I am building a PHP CMS from the ground up. There is one super-core

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.