/    Sign up×
Community /Pin to ProfileBookmark

Is there any advantages of storing database connection in static member variable instead of non static one that is encapsulated in singleton pattern? I just want to make sure that there isn’t any before I implement.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 05.2010 — While I will not say to never use singletons, before using them, you should read the arguments against it: http://www.google.com/search?q=why+singletons+are+evil

Personally, I tend to create a single database object (usually based on or extending the MySQLi or PDO class), and pass it to the constructors of objects which need it. I guess I prefer the visibility that provides in the interface.

If you prefer to invoke it from within each class from a global source, then I suspect a static variable might be a bit more efficient than invoking a singleton object in each class that needs it, but that's only a guess.
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 05.2010 — Just wanted to comment that professional PHP developers should note singletons are not unit testable. Doing so violates "test independence", a fundamental requirement for unit testing via tools like PHPUnit and SimpleTest.

FYI for those who follow SDLC and life cycle testing.

-jim
Copy linkTweet thisAlerts:
@SyCoJan 06.2010 — "life cycle testing"

10 write code

20 release

30 phone rings

40 fix bugs

50 goto 30

?
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 07.2010 — ??? So true!
Copy linkTweet thisAlerts:
@MindzaiJan 07.2010 — "life cycle testing"

10 write code

20 release

30 phone rings

40 fix bugs

50 goto 30

?[/QUOTE]


goto? tut tut :p
Copy linkTweet thisAlerts:
@homer_j_simpsonauthorJan 08.2010 — Thanks for the input guys.

"life cycle testing"

10 write code

20 release

30 phone rings

40 fix bugs

50 goto 30

?[/QUOTE]


lol spot on. why bother when you can fix it later on. ?
×

Success!

Help @homer_j_simpson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...