php - Global connection variable or new one every time -


i'm thinking of creating global variable $pdo use along script inside classes , functions , code . there security breach can occur upon ?
i'm using seperate connection every time . shall stick or use global pdo object ?

this depends on application, there cases want manage multiple connections. however, in many cases, makes sense open single connection use during course of processing single request. helps eliminate overhead of opening/closing connections throughout code. guess security not of concern in cases unless planning on handing off db connection set of code should not have db permissions associated specific connection (maybe library or service not control).

i highly suggest concept of dependency injection, in cases preferable pass around instantiated db object pieces of code need rather treat global , use global keyword access it. helps avoid needing replicate code in every class/function check db has connection , such. if have single class , can pass object around classes need it, can guaranteed object supposed do.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -