On Jan 10, 2008 1:33 PM, <reese@xxxxxxxxxxxxxxxx> wrote: > I have been using define to create a constant for the link resource returned by mysql > pconnect like so: > > $PL = @mysql_pconnect("localhost", $DBUser, $DBPass); > define("SITE_DB",$PL); > > > Later I use the constant to select my databases. > > mysql_select_db($SrcdbID ,SITE_DB); > > This code seems to be working as I expected and I have many thousands of llines of code > done over several years using this construct. > > But, I happened to be reading the php doco today and noticed that you are not supposed to > use define for resources, so question is, is what I am doing safe or am I going to run into > problems and if so what is the best way to globally pass resources to multiple classes and > functions, command line scripts etc? > > > Cheers > Charlie Reese > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Hi Charlie! Well a CONSTANT is a value that doesn't change. I don't think that makes sense for a DB connection. The connection command returns a resource that could be different on each request depending on if you've worked on different resources. You could just do this: $GLOBALS['SITE_DB'] = @mysql_pconnect("localhost", $DBUser, $DBPass); Then inside your function reference it like this: mysql_query($sql, $GLOBALS['SITE_DB']); This would be an easy mass find/replace that you could do. Global variables are evil though. I personally use the registry pattern to hold an instance of the database connection that can be lazy loaded whenever I need it. Then inside of my gateway objects I pass that instance in the constructor so they only have to know how to work with its interface. If you're okay with using object that might be a nice road to travel. You could also use the singleton pattern but I would advise against that because what if you wanted 2 instances for different databases? I've never ran across this myself but you never know. Plus it makes unit testing a little tricky since you can't mock it as easily. Hopefully something in all this will help you to an answer. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php