lpowerl Posted September 28, 2015 Share Posted September 28, 2015 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 28, 2015 Share Posted September 28, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.