Force Single Objects in a Container

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:05
    Home stretch. Our goal is to make Container
  • 0:08
    responsible for creating every service object,
  • 0:12
    like PDO, but also ShipLoader and BattleManager.
  • 0:16
    Here's our issue. If we called $container->getPDO
  • 0:20
    twice on the same request,
  • 0:22
    we'd still end up with multiple PDO objects.
  • 0:26
    And so multiple database connections.
  • 0:29
    OK. If we're careful, we can avoid this.
  • 0:33
    We can do better. Let's guarantee
  • 0:36
    that only one PDO object is ever created.
Loading ...

Force Single Objects in a Container

Loading...

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.

Downloads: 
Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.