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?
$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
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:
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?