# Trying to inject PDO with failing when using type hinting

**URL:** https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663
**Category:** Questions
**Created:** [February 18, 2021, 7:14pm UTC](https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663 "2021-02-18T19:14:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![carlosrpnegrao](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@carlosrpnegrao](https://discourse.slimframework.com/u/carlosrpnegrao)
#### Post date: [February 18, 2021, 7:14pm UTC](https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663/1 "2021-02-18T19:14:00Z")

</div>

I’ve spent the whole afternoon trying to get DI to work with PDO, but for what’s probably a dumb reason it didn’t work, so, can I ask for a bit of help here?

```php
     $builder = new DI\ContainerBuilder();
    $builder->addDefinitions([
        PDO::class => function (ContainerInterface $container) {
            $host = 'localhost';
            $dbname = 'xxx';
            $username = 'yyyy';
            $password = 'zzzz';
            $charset = 'utf8mb4';
            $flags = [
                // Turn off persistent connections
                PDO::ATTR_PERSISTENT => false,
                // Enable exceptions
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                // Emulate prepared statements
                PDO::ATTR_EMULATE_PREPARES => true,
                // Set default fetch mode to array
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                // Set character set
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
            ];
            $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
    
            return new PDO($dsn, $username, $password, $flags);
        },
    ]);
    $container = $builder->build();
    AppFactory::setContainer($container);

```

So far this works if I use

```auto
die(var_dump(DPO::class)); 

```

I can see that a PDO object has been created.

But when it come to the time to use it on a constructor:

```php
    abstract class Controller
    {
        private $db = null;
        public function __construct(PDO $db)
        {
            $this->db = $db;
        }
    }

```

I get the error:  
`Entry "PDO" cannot be resolved: Parameter $dsn of __construct() has no value defined or guessable`

What could I be doing wrong?

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [February 19, 2021, 1:02pm UTC](https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663/2 "2021-02-19T13:02:34Z")

</div>

I wonder why this line works for you:

> [@carlosrpnegrao](#):
>
> `die(var_dump(DPO::class)); `

This line gives me: `string(3) "DPO"`, but the output must be `object(PDO)#1 (0) {}`

> Entry “PDO” cannot be resolved: Parameter $dsn of \_\_construct() has no value defined or guessable

This error message says that the DI container could not find a container definition and tried to create it manually. So make sure that the DI container for `DPO::class` is correctly configured.

Example:

```php
use DI\ContainerBuilder;
use Slim\App;
use Slim\Factory\AppFactory;

$containerBuilder = new ContainerBuilder();

// Add container definitions
$containerBuilder->addDefinitions( /* ... */ );

// Build PHP-DI Container instance
$container = $containerBuilder->build();

// Create App instance
$app = $container->get(App::class);

// ...

$app->run();

```

Also make sure to set the correct container instance into the Slim AppFactory.  
I would also recommend putting the `App::class` into the DI container:

```php
App::class => function (ContainerInterface $container) {
    AppFactory::setContainer($container);

    return AppFactory::create();
},

```

---

<div class="post-metadata">

### Author: ![carlosrpnegrao](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@carlosrpnegrao](https://discourse.slimframework.com/u/carlosrpnegrao)
#### Post date: [February 19, 2021, 1:44pm UTC](https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663/3 "2021-02-19T13:44:50Z")

</div>

Here’s the configuration for the container:

> [@](#):
>
> > use Psr\Container\ContainerInterface;
> > 
> > use Slim\App;
> > 
> > use Slim\Factory\AppFactory;
> > 
> > use Slim\Middleware\ErrorMiddleware;
> > 
> > use Slim\Views\Twig;
> > 
> > use Slim\Views\TwigMiddleware;
> > 
> > return [
> > 
> > ```
> > 'settings' => function () {
> > 
> > return require __DIR__. '/config.php';
> > 
> > },
> > 
> > PDO::class => function (ContainerInterface $container) {
> > 
> > $settings = $container->get('settings')['db'];
> > 
> > $host = $settings['host'];
> > 
> > $dbname = $settings['database'];
> > 
> > $username = $settings['username'];
> > 
> > $password = $settings['password'];
> > 
> > $charset = $settings['charset'];
> > 
> > $flags = $settings['flags'];
> > 
> > $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
> > 
> > return new PDO($dsn, $username, $password, $flags);
> > 
> > },
> > 
> > Slim\Views\Twig::class => function(ContainerInterface $container) {
> > 
> > return Twig::create('/home/fmo/slim_fmo/templates', ['cache' => '/tmp/slim_fmo/cache']);
> > 
> > },
> > 
> > App::class => function (ContainerInterface $container) {
> > 
> > AppFactory::setContainer($container);
> > 
> > return AppFactory::create();
> > 
> > },
> > 
> > ```
> > 
> > ];

Here I attempt to create the app:

> [@](#):
>
> // Create Container
> 
> $containerBuilder = new DI\ContainerBuilder();
> 
> // Add container definitions
> 
> $containerBuilder-\>addDefinitions( **DIR**. ‘/…/config/container.php’);
> 
> // Build PHP-DI Container instance
> 
> $container = $containerBuilder-\>build();
> 
> // Create App instance
> 
> $app = $container-\>get(App::class);

Now it fails with

> [@](#):
>
> FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught DI\NotFoundException: No entry or class found for ‘App’ …

Before I had been using

> [@](#):
>
> AppFactory::setContainer($container);  
> $app = \DI\Bridge\Slim\Bridge::create();

so it failed once later, but I figured I’d better try to follow your example as closely as possible.

---

<div class="post-metadata">

### Author: ![carlosrpnegrao](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@carlosrpnegrao](https://discourse.slimframework.com/u/carlosrpnegrao)
#### Post date: [February 19, 2021, 2:37pm UTC](https://discourse.slimframework.com/t/trying-to-inject-pdo-with-failing-when-using-type-hinting/4663/4 "2021-02-19T14:37:04Z")

</div>

I think I may be stupid.

I used “$app = \DI\Bridge\Slim\Bridge::create();” since I saw it as being the recomended way to initialize the app after installing the PHP-DI Slim bridge, but I never noticed I didn’t pass it the container.

Using “$app = \DI\Bridge\Slim\Bridge::create($container);” solved the problem.
