I start a new slim project with twig support, I want to use PDO as database layer, what is the way to integrate? or just use GLOBAL $db?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
i’m against mutable globals. you could use a simple factory method returning your connection:
if you’re about to deal with more of such problems, consider a registry (see this one).
Regarding mutable globals:
I’m against such things because, well, they are easy to be mutated.
What happens if, during the flow of a program, circumstances change unexpectedly?
Things break.
If you’re the only one working on the project, globals might be ok.
But from my experience, even in this scenario, keeping track of global variables becomes a hassle and offends logical organisation of your code.
What if you come to the point of sharing your code with other who will overwrite such global?
Also, globals contradict the methodology of data encapsulation in the context of seperation of concerns.
All this plays into the big, wide science of software architecture.
Additionally, using a factory in this specific case ensures that you only keep one single reference to the connection to your database.