Module Development

Force Single Objects in a Container for Drupal 8, 9

Sprout Video

To wrap things up with our app, the last bit of housekeeping is to make one container responsible for creating every service object, like PDO, but also ShipLoader and BattleManager. The problem is that if we called $container->getPDO() twice on the same request, we'd still end up with multiple PDO objects, and so, multiple database connections. Ok, if we're careful, we can avoid this. We can do better though—let's guarantee that only one PDO object is ever created. We did this previously in ShipLoader, so now we'll move this into our container.