Re: Opinion about the using $GLOBALS directly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux