Thursday, 15 July 2010

oop - Using Singletons in PHP -


I want to use a DB singleton in several classes of classes. Is it possible for each method to institute singleton individually, or to make it instant through a __ constructor () and access it with a variable in each method?

Thank you.

Keeping this in mind, a result will be similar: only one example of your DB object will be , And it's always the same.

Therefore, both solutions will work, and produce the same result; At least, if your DB class is made an example of your class every time - and obviously, the performance difference might be negligible.


Still, getting a singleton example in constructor with the solution and using the class variable in other methods is a benefit: your methods will not depend on this singleton, but only one On the class variable ...

... which means it will be very easy (if you want to use only one day then you can modify your __ Construction method And can not do every method of the class) For example, to "fake" "deep" for automated testing purposes.

For more information, you can view this blog post:


No comments:

Post a Comment