title: How to set up and inject multiple PDO database connections in slim 4?
body: <p>I could make an instance of <code>PDO</code> and inject it successfully. I defined the <code>PDO::class</code> directly and injected it in the constructor with <code>__construct(PDO $pdo)</code>. I would need something like <code>PDO1::class</code> and <code>PDO2::class</code> to inject it like follows: <code>__construct(PDO1 $pdo1, PDO2 $pdo2)</code> but that obviously doesn't work. There is only one <code>PDO</code> class and what I need to do is 2 instances of it with different database credentials.<br>
  What is the best way to do it?</p>
  
  <p>I set up one definition of a database via PDO like this and it works:  </p>
  
  <p>File: <code>dependencies.php</code></p>
  
  <pre class="lang-php prettyprint-override"><code>use DI\ContainerBuilder;
  use Psr\Container\ContainerInterface;
  
  return function (ContainerBuilder $containerBuilder) {
      $containerBuilder-&gt;addDefinitions([
          PDO::class =&gt; function (ContainerInterface $c) {
              $dbSettings = $c-&gt;get('settings')['db1'];
              $dsn = 'mysql:host=' . $dbSettings['host'] . ';dbname=' . $dbSettings['dbname'];
              $options = [
                  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,
                  PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,
                  PDO::ATTR_EMULATE_PREPARES =&gt; false,
              ];
              return new PDO($dsn, $dbSettings['user'], $dbSettings['pass'], $options);
          },
      ]);
  };
  </code></pre>
  
  <p>File: <code>index.php</code></p>
  
  <pre class="lang-php prettyprint-override"><code>...
  // Set up dependencies
  $dependencies = require __DIR__ . '/../app/dependencies.php';
  $dependencies($containerBuilder);
  // Build PHP-DI Container instance
  $container = $containerBuilder-&gt;build();
  // Set container to create App with on AppFactory
  AppFactory::setContainer($container);
  // Instantiate the app
  $app = AppFactory::create();
  ...
  </code></pre>
  
  <p>File <code>SomeRepository.php</code></p>
  
  <pre class="lang-php prettyprint-override"><code>use PDO;
  
  class SomeRepository{
  
      protected $pdo;
  
      public function __construct(PDO $pdo) {
          $this-&gt;pdo = $pdo;
      }
  }
  </code></pre>
  
  <p>I've seen something like this in <a href="https://arjunphp.com/connect-multiple-database-slim-framework/" rel="nofollow noreferrer">this</a> article:</p>
  
  <pre class="lang-php prettyprint-override"><code>return function (ContainerBuilder $containerBuilder) {
      $containerBuilder-&gt;addDefinitions([
          'db1' =&gt; function (ContainerInterface $c) {
              $db1Settings = $c-&gt;get('settings')['db1'];
              $dsn = 'mysql:host=' . $db1Settings['host'] . ';dbname=' . $db1Settings['dbname'];
              $options = [ ... ];
              return new PDO($dsn, $db1Settings['user'], $db1Settings['pass'],$options);
          },
          'db2' =&gt; function (ContainerInterface $c) {
              $db2Settings = $c-&gt;get('settings')['db2'];
              $dsn = 'mysql:host=' . $db2Settings['host'] . ';dbname=' . $db2Settings['dbname'];
              $options = [ ... ];
              return new PDO($dsn, $db2Settings['user'], $db2Settings['pass'],$options);
          },
  
      ]);
  };
  </code></pre>
  
  <p>But is it the best way to do it? And how can I access the connections in a repository class without having to inject the whole container?</p>