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

  • Home
  • SEARCH
  • 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 8812645
In Process

The Archive Base Latest Questions

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

As of right now I have a database connection string included at the top

  • 0

As of right now I have a database connection string included at the top of each of my pages. I’m then passing my database connection to the methods in my class like this:

public function select($db) {
  //Code here
}

Code on page:

$login_user->select($db);

My thought is that if I ever want to query a different database I can just create a new connection string in my include file called $db2 and then I just pass that value instead of $db.

Is this a standard way of doing this or do you have a different recommendation?

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

    Passing a connection string to your classes has lots of disadvantages and no benefits. You are on the right track, but you want to pass the database object instead of a connection string.

    Dependency Injection is a good way of giving your classes access to the database, which simply means to pass dependencies (ie database object) to the objects that need them, rather than the object itself obtaining the dependency from a global variable of some kind.

    I would suggest that you use a method like setDb() on your classes to pass the database objects, and then store it as a property for any internal use.

    For example, assuming you have created the database object $db in an initialisation script:

    class SomeClass
    {
        protected $db;
    
        public function setDb($db)
        {
            $this->db = $db;
        }
    
        public function something()
        {
            // do some query on the database using $this->db
        }
    }
    
    $obj = new SomeClass();
    $obj->setDb($db);
    
    $obj->something();
    

    DI gives you the benefits that you mentioned: the ability to easily switch the db without having to do lots of work in your methods. There are other benefits, namely ease of testing.

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

Sidebar

Related Questions

Right now, I have a database with around 20 columns and 70 rows. I
Right now, I have a database field named 'PROCESS_FLAG' that contains the text value
Right now I have a function, in a class that is used to listen
I have a set of static class methods. I also have an existing database
Right now I have my program running on my SQLExpress database. But what I'd
I have a console application that uses a database connection string which is defined
I'm trying to create a faster query, right now i have large databases. My
Good day! I right now have a function the drags an element from a
Right now I have an upload field while uploads files to the server. The
Right now I have a script that will get the last five files in

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.