11.
Service Container
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
2 Comments
@Brontie tell me the highlights of this chapter
This chapter’s big win is centralizing object creation with a hand-built
Containersoindex.phpandbattle.phpstop duplicatingnew PDO()setup. TheContainer::getPDO()method becomes the single place that knows how to build the database connection, which makes future changes much easier. It also moves the database settings into the container’s constructor, so the connection logic can use shared configuration instead of hardcoded values scattered across entry scripts.The key implementation pattern is:
The one gotcha worth remembering: passing
$configurationinto__construct()is not enough — you must assign it to$this->configuration, orgetPDO()will still seenulland fail when it tries to create the connection."Houston: no signs of life"
Start the conversation!