Jump to content

singleton pdo class with static methods


lpowerl

Recommended Posts

hi

is it really a good practice to have a pdo class with singleton patterns?

i read about this issue a lot and understand is not a proper way to make a database class because it makes unit test hard !

the second question is about static form to write php codes.

is it necessary to use static methods in our project cause it makes hard for me to write?

when we should use static methods in our projects ?

 

thanks.

Link to comment
Share on other sites

If the singleton represents a database connection, the biggest problem is that you'll never be able to establish a second connection within the same script.

 

You may not need multiple connections right now, but that can change in the future. For example, if you decide to store your PHP sessions in the database, the only safe solution is to have an additional database connection just for the session. Now what? You'd have to either copy-and-paste your entire class to create a new singleton, or you'd need a complete rewrite of your database-related code.

 

So this is not future-proof. And testing is indeed a problem as well.

 

 

 

is it necessary to use static methods in our project cause it makes hard for me to write?

when we should use static methods in our projects ?

 

This question is far too vague. You should provide a concrete example so that we can comment on this specific case.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.