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

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

 




----- Original Message ----- From: "Andreas Korthaus" <akorthaus@xxxxxx>
To: <php-general@xxxxxxxxxxxxx>; "Gustav Wiberg" <gustav@xxxxxxxxxxxxxx>
Sent: Friday, March 03, 2006 8:53 PM
Subject: Re:  Coding Practice: Use global $var or pass in by refernce


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

Hi Andreas!

I don't have that much experience with classes, but wouldn't it work to:

1. make a connection to the db like $db = <open db>, and then pass around this variable?

2. Extend the DB - class, so the saveChangesInDb() - function in the Foo-class would be an extension from the DB-class
? (I think this works to extend a class in PHP right?)

3 . Use already existing classes for db...http://www.phpclasses.org/ PHP Scripts / Databases

I hope this'll help!

/G

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