aharvilla Posted July 16, 2010 Share Posted July 16, 2010 I am having trouble understanding the singleton design pattern in order to setup a DB object. My problem is that I dont think I am trying to use it in the correct way which might have answered my problem to begin with. Even so, I still want to know if it is possible when starting my page to 1. instantiate the object and then 2. Use that instantiated DB object by OTHER objects instantiated after in some way. I apologize if I might be forgetting something but from what I understand, this is what my idea of the singleton design pattern. Would i have to pass the Object to a class or a function as a parameter or something like that or can i access the object directly from another class without having to pass any kind of anything. Quote Link to comment https://forums.phpfreaks.com/topic/207966-singleton-db-design-pattern/ Share on other sites More sharing options...
KevinM1 Posted July 16, 2010 Share Posted July 16, 2010 I am having trouble understanding the singleton design pattern in order to setup a DB object. My problem is that I dont think I am trying to use it in the correct way which might have answered my problem to begin with. Even so, I still want to know if it is possible when starting my page to 1. instantiate the object and then 2. Use that instantiated DB object by other objects in some way. I apologize if I might be forgetting something but from what I understand, this is what my idea of the singleton design pattern. Would i have to pass the Object to a class or a function as a parameter or something like that or can i access the object directly from another class without having to pass any kind of anything? You'd have to access it via its static getInstance() method. Something like: $db = Database::getInstance(); // do stuff with $db Remember: with a Singleton, getInstance either instantiates the db and returns that new instance OR returns the existing instance. To get your hands on a Singleton's cookies, you need to call its static method. Quote Link to comment https://forums.phpfreaks.com/topic/207966-singleton-db-design-pattern/#findComment-1087179 Share on other sites More sharing options...
aharvilla Posted July 16, 2010 Author Share Posted July 16, 2010 I thank you for the speedy reply! I understand about the static method and private constructor but i want to know if i can access the $db variable in another instantiated class. If i am making this confusing i apologize !? Quote Link to comment https://forums.phpfreaks.com/topic/207966-singleton-db-design-pattern/#findComment-1087187 Share on other sites More sharing options...
AbraCadaver Posted July 16, 2010 Share Posted July 16, 2010 Yes, that was Nightslyr's point: class something { function somefunc() { $db = Database::getInstance(); // do stuff with $db } } Quote Link to comment https://forums.phpfreaks.com/topic/207966-singleton-db-design-pattern/#findComment-1087190 Share on other sites More sharing options...
aharvilla Posted July 16, 2010 Author Share Posted July 16, 2010 Thank you guys! This is my first or second time using this forum and am very pleased! Quote Link to comment https://forums.phpfreaks.com/topic/207966-singleton-db-design-pattern/#findComment-1087195 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.