Adam Posted January 8, 2010 Share Posted January 8, 2010 Turning the 'OOP conventions' topics into a trilogy I guess, how would you (or what's your preferred method to) implement a singleton design pattern? Mainly interested in how you'd reference the class file and name. A Zend style approach or perhaps something a little more bespoke? Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/ Share on other sites More sharing options...
ignace Posted January 8, 2010 Share Posted January 8, 2010 I would avoid it. http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990912 Share on other sites More sharing options...
Adam Posted January 8, 2010 Author Share Posted January 8, 2010 I see. Is this a shared opinion? My knowledge in this area is quite limited to be fair, which why I asked really. --- Allow me to re-phrase the question, how would you go about object instantiation? Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990918 Share on other sites More sharing options...
Daniel0 Posted January 8, 2010 Share Posted January 8, 2010 My favorite way of handling the singleton pattern is by avoiding it. Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990919 Share on other sites More sharing options...
Adam Posted January 8, 2010 Author Share Posted January 8, 2010 Yeahh fuck, was way off with this topic. Let's ignore it 'eh! Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990925 Share on other sites More sharing options...
ignace Posted January 8, 2010 Share Posted January 8, 2010 how would you go about object instantiation? A factory or builder in DD terms Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990966 Share on other sites More sharing options...
dgoosens Posted January 8, 2010 Share Posted January 8, 2010 Well there are some places where a singleton comes in handy... doesn't it ? MrAdam talks about ZF... So Zend_Registry is quite useful I believe I find it very useful to make sure there is only one instance for the DB connection. I know you have to be careful with them though and don't use them everywhere... Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990970 Share on other sites More sharing options...
Daniel0 Posted January 8, 2010 Share Posted January 8, 2010 I find it very useful to make sure there is only one instance for the DB connection. So what happens when you need *drumroll*... two connections? Answer: You're screwed. And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it? Answer: You're screwed. And what happens when you want to change from MySQLWhatever to PostregeSQLWhatever, but the choice of adapter is hardcoded? Answer: You're screwed. Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990975 Share on other sites More sharing options...
Garethp Posted January 8, 2010 Share Posted January 8, 2010 What are Signletons? Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990976 Share on other sites More sharing options...
dgoosens Posted January 8, 2010 Share Posted January 8, 2010 So what happens when you need *drumroll*... two connections? Answer: You're screwed. although I don't see the point of having two connections for the same DB... in that case you would not work with a singleton... obviously... And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it? Answer: You're screwed. nope... cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production... And what happens when you want to change from MySQLWhatever to PostregeSQLWhatever, but the choice of adapter is hardcoded? Answer: You're screwed. sorry, but you're wrong here... Idealy you would not hardcode this kind of stuff in your models. Instead you would use an abstraction layer to handle this... Ergo, one modification in your config file and you can jump from a MySQL to a PostgreSQL or to whatever DB you'd like. Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990983 Share on other sites More sharing options...
ignace Posted January 8, 2010 Share Posted January 8, 2010 What are Signletons? A Singleton is a design pattern defined by the Gang of Four (GoF) to keep only one instance of an object. class Singleton { private static $_instance = null; private function __construct() {} // disable instantiation private function __clone() {} // disabe cloning public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } } And that's his implementation. Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990984 Share on other sites More sharing options...
Daniel0 Posted January 8, 2010 Share Posted January 8, 2010 And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it? Answer: You're screwed. nope... cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production... MysqlStuffBlaBla::getInstance() That's a singleton. I suppose you could do this: $adapter = 'Mysql'; $class = $adapter . 'StuffBlaBla'; $foo = $class::getInstance(); // or $foo = call_user_func(array($adapter . 'StuffBlaBla, 'getInstance')); But that's some ugly looking shit. I think you're talking about a factory. Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990987 Share on other sites More sharing options...
dgoosens Posted January 8, 2010 Share Posted January 8, 2010 And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it? Answer: You're screwed. nope... cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production... MysqlStuffBlaBla::getInstance() That's a singleton. I suppose you could do this: $adapter = 'Mysql'; $class = $adapter . 'StuffBlaBla'; $foo = $class::getInstance(); // or $foo = call_user_func(array($adapter . 'StuffBlaBla, 'getInstance')); But that's some ugly looking shit. I think you're talking about a factory. oups... you're right... Zend_Db is a factory... sorry about that... Quote Link to comment https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/#findComment-990992 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.