php - Using database objects in functions -


in webapp building make use of pdo database object querying database. instantiated @ top of every page using custom class built based on pdo:

$db = new database; //database name of class. 

i use class in script:

$db->query("select field table"); $results = $db->resultset(); 

i have number of standalone functions need make use of database object.

my question is better practise pass $db function argument or globalize within function, given $db globally used variable. have heard things avoiding polluting global namespace.

i.e.

function myfunction($db, $a1, $a2){     //stuff } 

vs

function myfunction($a1, $a2){     global $db;     //stuff } 

in opinion, global vars practice. think should use first solution: passing db instance first parameters function.

if have 1 instance of database, better option make database class singleton. allow $db = database.getinstance(); in functions.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -