DyslexicDog Posted January 14, 2008 Share Posted January 14, 2008 I'm currently working on a project to convert a procedural based application into objects. my overall design is basically complete but I'm having problems with one design issue. I have a database object that I create at every page load. I would like to be able to reuse this object in other classes whenever necessary. What would be the best way to do this? I've tried a few different things but they turn into spaghetti mess very quickly. Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/ Share on other sites More sharing options...
Mirkules Posted January 21, 2008 Share Posted January 21, 2008 I don't think there is a way to maintain state of the objects. HTTP Requests by definition are stateless. On second thought, I wonder if it would be possible to serialize your object into a cookie But that would raise all sorts of problems, like security issues, what happens if the client doesn't have cookies enabled, etc... Out of curiosity, what were your previous attempts at this? Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-444733 Share on other sites More sharing options...
corbin Posted January 21, 2008 Share Posted January 21, 2008 It's not possible to serialize a resource. And, I don't know if DyslexicDog (genius name... wish I was creative haha) meant retain the object through multiple pages. I think he was looking for something like this: class Database { } class OtherClass { private $db = false; function __construct(&$db) { $this->db = $db; } } class OtherOtherClass { private $db = false; function __construct(&$db) { $this->db = $db; } } $db = new Database; $otherclass = new OtherClass($db); $otherotherclass = new OtherOtherClass($db); Or.... You could do it this way if you want a more lazy approach: class Database { } GetDatabase()->SomeMethod(); //This might require PHP 5... not sure. function GetDatabase() { static $db = false; if($db === false) { $db = new Database(); } return $db; } You could also have a main class that spawns the sub classes and so on.... Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-444857 Share on other sites More sharing options...
Liquid Fire Posted January 21, 2008 Share Posted January 21, 2008 The second approach is better than the first one however do something like this /** * @desc returns the instance of the object * * @author Ryan Zec * * @param void * @return void */ public static function get_instance() { $configuration = configuration::get_instance(); if(!isset(self::$instance)) { self::$instance = new database($configuration->get_sql_host(), $configuration->get_sql_username(), $configuration->get_sql_password(), $configuration->get_sql_database(), $configuration->get_sql_type()); } return self::$instance; } and then in each class $db = database::get_instance(); This allows you do get the same database object(making it a singleton) and you don't have to worry about passing the object by reference every time you create a new object. Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-445056 Share on other sites More sharing options...
corbin Posted January 21, 2008 Share Posted January 21, 2008 Yeah, I see how that way would be better.... That way you wouldn't have a bunch of random functions floating around your script, and it would help you stay OO. Hrmmm I'm not that into OOP ;p lol. Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-445307 Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 Instead of creating a lot of singletons you might want to make a registry instead. I believe the resources topic in the app design forum has some links regarding that. Otherwise you could just google it. You should still try to pass an object as an argument if possible though. Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-445544 Share on other sites More sharing options...
Liquid Fire Posted January 22, 2008 Share Posted January 22, 2008 I still prefer the singleton method because the registry does not give me anything special I would want(or at least not tutorial or explanation has show me anything useful). Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-445784 Share on other sites More sharing options...
DyslexicDog Posted February 7, 2008 Author Share Posted February 7, 2008 Instead of creating a lot of singletons you might want to make a registry instead. I believe the resources topic in the app design forum has some links regarding that. Otherwise you could just google it. You should still try to pass an object as an argument if possible though. Sorry for the delay I basically fell off the earth for the last couple weeks. I like the singleton design idea, Why do you recommend a registry over a singleton and passing the database object overall? ' Quote Link to comment https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/#findComment-461110 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.