js schreef: > Hi Jochem, > > Sorry, I missed "static". > So, getDB() would works like singleton, right? yes - and it was just pseudo code off the top of my head, also there are 2 slightly different ways to attack this - one is to return an object that contains all the DB functionality (and stores the dbh internally) and the other is to return the actual dbh whether you need it ... the choice of which way to do this is rather dependant on how your current code works - regardless the point is the same - use a central 'store' for the dbh/db-object but keep it out of the global scope (so that there is less scope pollution and no chance of overwriting the dbh/db-object var) > I agree that's better method to manage dbh. :-) > > > On Dec 19, 2007 11:27 PM, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote: >> js schreef: >>> That wouldn't work well when you have to update multiple tables in a >>> transaction. >>> I think it's more maintainable to use GLOBALS than passing around dbh >>> to classes/functions. >> getDB() is a function that returns a database connection wrapper object not >> a handle to a connection (that handle is assumed to be stored inside the object). >> >> it is also assumed that this connection object has a query method of some sort >> so you could do something like: >> >> myBigTransaction() { >> $db = getDB(); >> $db->trans(); >> $db->query(/* bla */); >> $db->query(/* bla */); >> $db->query(/* bla */); >> $db->commit(); >> } >> >> BUT regardless of that example you can still get rid of the global dbh by >> replacing references to $GLOBALS['my_dbh'] with something like getDBH() with >> no functional difference except you nolonger have global variables that >> are overrideable. >> >> if you don't understand what I am saying (as your last reply seemed to imply) >> then please let's just leave it at that. >> >> >>> On Dec 19, 2007 11:07 PM, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote: >>>> please reply to the list ... >>>> >>>> js schreef: >>>>> I always store database handler in $GLOBALS. >>>>> I think that's the best place to save request-level-global. >>>>> I wonder where other people save that kind of data. >>>> how about a static variable inside a function or a static member of a class. >>>> >>>> e.g. >>>> >>>> function getDB($args) { >>>> static $conn = array(); >>>> >>>> $key = serialize($args); >>>> if (!isset($conn[ $key ]) >>>> $conn[ $key ] = new DBConn($args); >>>> >>>> return $conn[ $key ]; >>>> >>>> } >>>> >>>>> On Dec 19, 2007 9:52 PM, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote: >>>>>> Sancar Saran schreef: >>>>>>> Hello list. >>>>>>> >>>>>>> I want know to you opinions about using $GLOBALS directly. >>>>>>> >>>>>>> like >>>>>>> >>>>>>> $GLOBALS['myString'] = 'test'; >>>>>>> $GLOBALS['myArray']['this'] = 'this'; >>>>>>> $GLOBALS['myArray']['that'] = 'that'; >>>>>>> $GLOBALS['myClassObj] = new SomeClass; >>>>>> there is no real difference between 'global $foo' and $GLOBALS['foo'], >>>>>> and the second is probably more maintainance friendly (as Rob pionted out) >>>>>> >>>>>> that said, avoid globals like the plague - sometimes you may come up with >>>>>> a situation where using a global is really necessary - such situations should >>>>>> be the exception rather than the rule, often if your thinking of using a >>>>>> global there is another way of doing it. jmho >>>>>> >>>>>> >>>>>>> Regards >>>>>>> >>>>>>> Sancar >>>>>>> >>>>>> -- >>>>>> PHP General Mailing List (http://www.php.net/) >>>>>> To unsubscribe, visit: http://www.php.net/unsub.php >>>>>> >>>>>> >> > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php