br0ken Posted August 23, 2009 Share Posted August 23, 2009 Hello All, I'm trying to write a efficient object orientated system. I've got a database class that I want to be usable by all classes. Is there an efficient way of doing it without declaring it as a global? Once a class has been instantiated is there a way to fetch that rather than declaring a new instance? Thanks Link to comment https://forums.phpfreaks.com/topic/171542-solved-global-class/ Share on other sites More sharing options...
corbin Posted August 23, 2009 Share Posted August 23, 2009 A factor pattern could work for you. (You might be quick to think the Singleton pattern would be perfect for you since it allows only one instance of a given object, but the Singleton pattern should only be used if you are 100% sure you NEVER will want a second instance.) Or, you could pass the $db object reference to every object. Either way could work, and it depends on how much you're abstracting things and so on as to which one I would go with. Passing the object: class Database { } class User { } class Item { } //Assume User handles user details and authentication or something like that.... //And assume Item is an item the user owns or is selling or something of that nature... $db = new Database; $user = new User($user_id, $db); $item = new Item($item_id, $db); Or, with a factory: class FactoryException extends Exception { } class SimpleFactory { private $data = array(); private function __construct() {} private function __clone() {} private static function GetInst() { static $inst; if($inst == null) { $inst = new SimpleFactory; } return $inst; } private function _set($key, $val) { $this->data[$key] = $val; } private function _get($key) { if(!isset($this->data[$key])) { throw new FactoryException("Data not found for key {$key}."); } return $this->data[$key]; } public static function set($key, $val) { self::GetInst()->_set($key, $val); } public static function get($key) { return self::GetInst()->_get($key); } } $db = new Database; SimpleFactory::set('db', $db); Then you could retrieve the database object anywhere with SimpleFactory::get('db'). Edit: There are problems with the factory approach though. Personally if I did that, I would use a factory specifically for holding Database objects, not just a general factory. There's always the chance that some code later on could decide to store something in 'db' in the factory. You could always make a non-overwriteable factory though. Link to comment https://forums.phpfreaks.com/topic/171542-solved-global-class/#findComment-904647 Share on other sites More sharing options...
br0ken Posted August 23, 2009 Author Share Posted August 23, 2009 Thanks for the quick reply Corbin. The class in mind was a database class so I think the singleton will work as I will always only ever want one instance. I will look into more. Thanks Link to comment https://forums.phpfreaks.com/topic/171542-solved-global-class/#findComment-904723 Share on other sites More sharing options...
corbin Posted August 23, 2009 Share Posted August 23, 2009 Even with a database class I advise against using a Singleton. I used to think that I would never want more than 1 instance of a database class too, but sure enough a while back I did a huge project using a Singleton database class.... Sure enough a month into the project the need to have two database connections open came up. Luckily I ghetto hacked the class I had been using to retrieve the database, but it would've been much more elegant/better code had I not had to do so. Link to comment https://forums.phpfreaks.com/topic/171542-solved-global-class/#findComment-904725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.