Using a singleton database class in functions and multiple scripts(PHP) - best use methods
        Posted  
        
            by dscher
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by dscher
        
        
        
        Published on 2010-03-12T07:00:05Z
        Indexed on 
            2010/03/12
            7:07 UTC
        
        
        Read the original article
        Hit count: 288
        
I have a singleton db connection which I get with:
$dbConnect = myDatabase::getInstance();
which is easy enough. My question is what is the least rhetorical and legitimate way of using this connection in functions and classes? It seems silly to have to declare the variable global, pass it into every single function, and/or recreate this variable within every function. Is there another answer for this?
Obviously I'm a noob and I can work my way around this problem 10 different ways, none of which is really attractive to me. It would be a lot easier if I could have that $dbConnect variable accessible in any function without needing to declare it global or pass it in. I do know I can add the variable to the $_SERVER array...is there something wrong with doing this? It seems somewhat inappropriate to me.
Another quick question: Is it bad practice to do this:
$result = myDatabase::getInstance()->query($query);
from directly within a function?
© Stack Overflow or respective owner