Re: uh oh, I defined a resoruce

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

 



On 1/10/08, Eric Butera <eric.butera@xxxxxxxxx> wrote:
>
> On Jan 10, 2008 2:28 PM, David Giragosian <dgiragosian@xxxxxxxxx> wrote:
> >
> > On 1/10/08, Eric Butera <eric.butera@xxxxxxxxx> wrote:
> > >
> > > On Jan 10, 2008 2:02 PM, Philip Thompson <philthathril@xxxxxxxxx>
> wrote:
> > > > On Jan 10, 2008, at 12:48 PM, Eric Butera wrote:
> > > >
> > > > > 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
> > > > >>
> > > > >>
> > > > > 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=global+variables+are+evil&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=rv83RnqqLJDobfnA3d78Lg&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNF3eQaMYwTOPeOFmY3o3_vKZoxD3g
> >
> > variables<
> http://www.google.com/url?sa=X&oi=dict&ei=InGGR9TUIYO6gATv4ZHKBg&sig2=PtlnDj6B_ES74-UoJFdoYg&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNHUllfcxecDiCBZ124r8p5KyQoZlQ
> >are
> > evil<
> http://www.google.com/url?sa=X&oi=dict&ei=InGGR9TUIYO6gATv4ZHKBg&sig2=QHGtFJrSLNf_0ARuYMWpHA&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNF5_Fag6ntgqNkFzuWVU9bEyb717Q
> >
> > *. (*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_hljiBXsA
> >
> > variables<
> http://www.google.com/url?sa=X&oi=dict&ei=83CGR832BpbAggSao-G9Bg&sig2=kto8MuCLr5saX5VPjN8ThQ&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNF1uZkzAKhsWmdBCI0IzFzgLSEAhA
> >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<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=Gorx1ErPpjplcNDVa0qb6g&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNFszs4YfNg7ok3-m08IgyrhO5EAsQ>
variable<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=vCOY79teeScIcrxuvb6k3g&q=http://www.answers.com/variable%26r%3D67&usg=AFQjCNEzQBZwfNnJ4pr1pnYOXYttJJNkvg>are
evil<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=eh8-cXxXIwGuHRJhXG2REQ&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNHcgNJwcVKoYcSB_Ks5OMr4LnAXWw>
"*
**
Hardly an overwhelming argument by using search results as a measure.

David

[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