Only One Database Connection with a Property

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:07
    I can't stand it any longer.
  • 0:09
    The app is small, but our database credentials are already duplicated
  • 0:14
    and hidden inside this one class.
  • 0:17
    What if we added a second table like battle
  • 0:20
    and a BattleLoader class?
  • 0:22
    At this rate, we'd be copying and pasting the database password there too.
  • 0:25
    Gross.
  • 0:29
    Enough is enough.
  • 0:31
    Let's fix this little by little.
  • 0:33
    First I don't want to duplicate the new PDO code twice in this class.
Loading ...

Only One Database Connection with a Property

Loading...

Our database is working well, but we currently have things set up so we are duplicating the database information, and that is going to be a nightmare to maintain. In this tutorial, we're going to clean up our database code by isolating the PDO creation in the ShipLoader class. Even with that in place though, we also need to make sure we only return one PDO object. We don't need a new database connection every time it gets called on a page. We'll fix that with a little bit of logic in our PDO code.

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.