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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:21:09+00:00 2026-06-10T11:21:09+00:00

I was reading this SO question: PHP – multiple different databases dependency injected class

  • 0

I was reading this SO question:

PHP – multiple different databases dependency injected class

Top answer. I understand the concept behind using an Interface here, but I don’t know how to use it. Here is what the top answer said, sorry if I’m not supposed to copy it here:

You should create an interface first for all the DB operations.

interface IDatabase
{
    function connect();
    function query();
    ...
}

Then have different driver classes implementing this interface

class MySQLDB implements IDatabase
{
}
class PGSQLDB implements IDatabase
{
}

This way you can easily use dependency injection.

class Test
{
   private $db;

   function __construct(IDatabase $db)
   {
        $this->db = $db;
   }
}

You can call it as:

$mysqldb = new MySQLDB();
$test = new Test($mysqldb);
or
$pgsqldb = new PGSQLDB();
$test = new Test($pgsqldb);

What I don’t understand is how to complete it in the class test and what I am passing to test. Where is my connection information going? I was hoping someone would help me complete this for a mysql connection or maybe pdo.

  • 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-10T11:21:11+00:00Added an answer on June 10, 2026 at 11:21 am

    Your connection info would go in the MySQLDB class, so you could have something like this:

    class MySQLDB implements IDatabase
    {
        private $pdo; // Holds the PDO object for our connection
    
        // Or you can remove the parameters and hard code them if you want
        public function __construct( $username, $password, $database) {
            $this->pdo = new PDO( '...'); // Here is where you connect to the DB
        }
    
        public function query( $sql) {
            return $this->pdo->query( $sql); // Or use prepared statments
        }
    }
    

    Then you instantiate it outside of the class:

    $db = new MySQLDB( 'user', 'pass', 'db');
    

    And pass that $db object to one of your classes expecting an IDatabase:

    $obj = new Test( $db); // Dependency Injection, woo hoo!
    

    You can also look into having the MySQLDB class extending the PDO class, but that is your design choice.

    Finally, you might be better off just sticking with PDO and getting rid of all this, as it is a great abstraction layer that works with many different databases.

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

Sidebar

Related Questions

I was reading this question, but didn't understand some part of the aix's answer
After reading and commenting on this question PHP Library for Keeping your site index
After reading this question , i saw the answer by Naveen containing a link
I was reading over this question and wondered if the accepted answer might also
After reading answer to this question: Make "make" default to "make -j 8" I
Thanks in Advance for reading and answer this question. I got button in asp
This question arose for me while reading the Sha1-PHP Manpage It says there: If
after reading this question i wonder if someone can help me understand how to
I'm asking the question after reading this article http://stevehanov.ca/blog/index.php?id=95 Also isn't it a penalty
After reading this question, I need to clear up some things. IQueryable<Customer> custs =

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.