View Single Post

  #9 (permalink)  
Old 01-10-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] uh oh, I defined a resoruce

On Jan 10, 2008 3:34 PM, David Giragosian <dgiragosian@gmail.com> wrote:
>
>
> On 1/10/08, David Giragosian <dgiragosian@gmail.com> wrote:
> >
> >
> > On 1/10/08, Eric Butera <eric.butera@gmail.com > wrote:
> > > On Jan 10, 2008 2:28 PM, David Giragosian < dgiragosian@gmail.com >

> wrote:
> > > >
> > > > On 1/10/08, Eric Butera <eric.butera@gmail.com> wrote:
> > > > >
> > > > > On Jan 10, 2008 2:02 PM, Philip Thompson < philthathril@gmail.com>

> wrote:
> > > > > > On Jan 10, 2008, at 12:48 PM, Eric Butera wrote:
> > > > > >
> > > > > > > On Jan 10, 2008 1:33 PM, < reese@adeptscience.com > 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
> > > > > > >>
> > > > > > >>
> > > > > > > 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.
> > > > > >
> > > > > >
> > > > > > I don't agree that "Global variables are evil." If you have a

> valid
> > > > > > purpose for defining a global variable, then go with it. The usual
> > > > > > argument is that it could accidentally be changed elsewhere (and

> there
> > > > > > are several more). However, defining a variable that you KNOW

> you're
> > > > > > going to use only for a database connection, IMO, is perfectly

> fine.
> > > > > >
> > > > > > $GLOBALS['SITE_DB'] = @mysql_pconnect("localhost", $DBUser,

> $DBPass);
> > > > > >
> > > > > > If you accidently change $GLOBALS['SITE_DB'] elsewhere, then I

> think
> > > > > > you have some bigger issues at hand. =P HTH.
> > > > > >
> > > > > > ~Philip
> > > > > >
> > > > > > "Personally, most of my web applications do not have to factor

> 13.7
> > > > > > billion years of space drift in to the calculations, so PHP's rand
> > > > > > function has been great for me..." ~S. Johnson
> > > > > >
> > > > > > --
> > > > > >
> > > > > > PHP General Mailing List ( http://www.php.net/ )
> > > > > > To unsubscribe, visit: http://www.php.net/unsub.php
> > > > > >
> > > > > >
> > > > >
> > > > > Depending on what project I'm working on sometimes I use specific
> > > > > globals on our older procedural code because:
> > > > >
> > > > > 1) It is our code
> > > > > 2) It does not use outside code
> > > > > 3) I'm the only developer working on it
> > > > > 4) It is very simplistic
> > > > >
> > > > > By specific globals I mean that I have a "namespaced" entry such as
> > > > > $GLOBALS['ericonly']['db'] and put things inside of ericonly. I
> > > > > basically only use this for database connections and a few other

> very
> > > > > specific things.
> > > > >
> > > > > Aside from that I'm not going to argue the merits of globals.

> Someone
> > > > > else can do that as I've made up my mind and so have you. :)
> > > > >
> > > > > But just to prove that I'm right
> > > > >

> http://www.google.com/search?hl=en&q...il&btnG=Search
> > > > > :D
> > > >
> > > >
> > > > Well...
> > > >
> > > > Personalized Results *1* - *10* of about *121,000* for
> > > > *global<

> http://www.google.com/url?sa=X&oi=dict&ei=InGGR9TUIYO6gATv4ZHKBg&sig2=rv 83RnqqLJDobfnA3d78Lg&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNF3eQaMYwTOPeOFmY3o3_vKZo xD3g>
> > > > variables<

> http://www.google.com/url?sa=X&oi=di...124r8p5KyQoZlQ
> >are
> > > > evil<

> http://www.google.com/url?sa=X&oi=dict&ei=InGGR9TUIYO6gATv4ZHKBg&sig2=QH GtFJrSLNf_0ARuYMWpHA&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNF5_Fag6ntgqNkFzuWVU9bEyb71 7Q>
> > > > *. (*0.10* seconds)
> > > >
> > > > Personalized Results *1* - *10* of about *4,330,000* for
> > > > *global<

> http://www.google.com/url?sa=X&oi=dict&ei=83CGR832BpbAggSao-G9Bg&sig2=b4uu-ZcrCZM0QWEXalxTNA&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNH3VaZGgornyBLBavFsd_hlji BXsA>
> > > > variables<

> http://www.google.com/url?sa=X&oi=di...CI0IzFzgLSEAhA
> >are
> > > > good<

> http://www.google.com/url?sa=X&oi=dict&ei=83CGR832BpbAggSao-G9Bg&sig2=ntLt-I0PIDJMXBMCk4OXCg&q=http://www.answers.com/good%26r%3D67&usg=AFQjCNHkthOJTyoPGlW9T2kzBpRwIqBc _A>
> > > > *. (*0.25* seconds)
> > > >
> > > > Sorry, I couldn't resist. ;-)
> > > >
> > > > David
> > > >
> > >
> > > Nice try, but if you don't put your query in quotes "global variabels
> > > are evil" then it is just finding searches that contain the words such
> > > as the first result:
> > >
> > > Global Variables Are Bad
> > > A good example of a global variable is LoginId? (or NetworkId?)
> > > whereby the app is making a lot of security or role-based calls and
> > > must pass this all over ...
> > > c2.com/cgi/wiki?GlobalVariablesAreBad - 24k -
> > >

> >
> >
> >
> > While "global variables are good" in quotes did not return any results

> from Google, "global variables are evil" in quotes only returned a result
> set of 4:
> >
> > Results 1 - 4 of 4 for " global variable are evil"
> >
> > Hardly an overwhelming argument by using search results as a measure.
> >
> > David

>
>
> Nevermind. I forgot the 's'.
>
> Once again:
>
> Personalized Results 1 - 8 of 8 for " global variables are good". (0.17
> seconds)
>
> Personalized Results 1 - 10 of about 136 for " global variables are evil".
> (0.07 seconds
>
> Ok. So those of us who use globals are 17 time more likely to end up in
> hell.
>
> I can live with those odds.
>
> David
>
>
>
>
>
>


Haha. Thank you for all that insightful research. Seriously though,
using globals you might already be in hell! =\
Reply With Quote