Re: Coding Practice: Use global $var or pass in by refernce

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

 



I would go for:

- you can create a new instance ("new DB") in every methode (but you
  usually only want a single DB-connection per script, and where do you
  pass config-data to access the DB?) or

This way the code keeps well organized and about the use of only one connection I wouldn't worry too much since usually one thread/process is created per script called, so if this is a multiuser application you end up with several connections anyway, besides I think PHP reuses the connections when possible so the performance of your application does not seem affected at all.

I use it, works perfectly with big databases and several users.

Regards.

Andreas Korthaus wrote:
Hi Gustav!

Gustav Wiberg wrote:

My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it!

Ok, so what's your recommendation to solve the problem with using a DB class in many other objects/methodes? Think of a DB class:

class DB {...}

And a lot of classes which want to use the DB class:

class Foo {
  function saveChangesInDb() {...}
}

class Bar {
  function saveChangesInDb() {...}
}

- You can use a global "$db = new DB..." and pass it to every class/methode,

- you can make $db "global" in each methode,

- you can create a new instance ("new DB") in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or

- use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?).


So what's the way you'd recommend and why?


best regards
Andreas


--
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