mysql - Multiple Database Connections PHP -


i'm having trouble figuring out solution problem. have multiples classes on site handle different things. have comment class, profile class, login class , on , forth. i;m happy code design far because logic out of html template. have minimal logic in template. problem is, have single class database connection, , extend every class database class , use parent constructor connect database. problem every page has @ least 2 separate classes on , site getting more complicated , i'm having 4 or 5 classes per page, it's noticeably effecting load time since have reconnect database each time.

my question what's best way use database connections in class. i'm thinking maybe database queries have handled in html, or create whole function file included on every page has database connection loaded it. i'd know how more experienced programmers can fix right way.

thanks advice.

although use singleton sure 1 connection used, seems holy war declared on stack overflow honest pattern.

nevertheless, extending application classes db class bad idea. these classes have nothing in common. database class service - so, use service. of classes have send emails sure - don't extend classes email class too?

you have instantiate db class once, , pass object other classes in constructors. easiest way use global $db;, or can bother passing them in constructor parameters. or use advertized dependency injection approach.

anyway, should use single connection (with same credentials) throughout whole script execution. opening , closing connection many times no better having multiple simultaneous connections.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -