Re: Coding Practice: Use global $var or pass in by refernce
| From: | Paul Scott | Date: | Sat, 04 Mar 2006 06:58:14 +0000 |
| Subject: | Re: Coding Practice: Use global $var or pass in by refernce | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-231441@lists.php.net to get a copy of this message | ||
On Fri, 2006-03-03 at 16:19 -0500, tedd wrote:
> Perhaps I'm fortunate, but I usually find a way around using Globals.
> And since I've been coding in PHP, I've never been forced to use them.
>
Just my 2c...
In our framework, I use globals as well, although I do tend to agree
with Tedd regarding workarounds rather... :)
Something like so:
I create a global to hold the db object (in my case MDB2 or PEAR DB) and
then use a loadclass function to get the dbconfig from another module.
This creates a nice way to easily pick it up wherever its needed. I then
use the factory method to instantiate the db abstraction layer and pass
it, through my "engine class" to an "object class" where I further
abstract the db functions through a central db class. All of the modules
in the system that have database derived classes extend my dbTable
class, thereby keeping everything centralised.
In dbTable class, I evaluate if the db object exists, and if it *really*
doesn't, I create it through the engine. This makes for "lazy"
evaluation of the db object, so that it works kinda on demand, rather
than on every request.
Using this method, I can also use a global error callback method to
handle _all_ database errors gracefully, using PEAR_Error. I do think
that there may be a slight performance hit doing it this way, but in an
app with over 180 000 lines of code (so far) I find it works just
fine...
--Paul