Globals are bad! But should I use them in this context?
Would setting the $link to my database be one thing that I should use a GLOBAL scope for? In my setting of (lots of functions)...it seems as though having only one variable that开发者_JAVA技巧 is in the global scope would be wise.
I am currently using the functions to transfer it back and forth so that way I do not have it in the global scope. But it is a bit of a hindrance to my script.
Please advise.
Your best way to accomplish this is by using a function as there already global.
function getDB()
{
global $Database;
Return $Database;
}
Then throughout you application use
getDB()->query(...)
Or if your just calling the resource then
mysql_query(...,getDB());
Personally, I think the database is one place where the Singleton
pattern makes sense. Testing fanatics might disagree with me, but it makes so much sense to me in this instance.
$db = DB::get();
$db->query('select ...');
Globals are very bad instead use the singleton pattern.
<?php
class Singleton
{
static private $instances = array();
static public function getInstance($className)
{
if (!isset(self::$instances[$className])) {
self::$instances[$className] = new $className();
}
return self::$instances[$className];
}
}
require_once 'DB.php';
$db = Singleton::getInstance('DB');
$db->query("select * .......");
?>
You can always access globals inside functions when you really need them.
function use_that_link() {
global $link;
var_dump($link);
}
But passing it around is probably better practice, regardless. Just remember that any function that uses direct database access takes the database link as its first parameter, and you should be good to go. (OOP might offer you an even cleaner solution, but it sounds like you don't want to venture down that path at this point.)
A more pragmatic approach: rudimentary namespacing
$GLOBALS['mydomain.com']['db'] = ...;
You can make a wrapper class for accessing and mutating your globals
Class Globals {
private $domain = 'mydomain.com';
function get($key)
{
return $GLOBALS[$this->domain][$key];
}
function set($key,$value)
{
$GLOBALS[$this->domain][$key] = $value;
}
}
class Registry
{
/*
* @var array Holds all the main objects in an array a greater scope access
* @access private
*/
private static $objects = array();
/**
* Add's an object into the the global
* @param string $name
* @param string $object
* @return bool
*/
public static function add($name,$object)
{
self::$objects[$name] = $object;
return true;
}
/*
* Get's an object out of the registry
* @param string $name
* @return object on success, false on failure
*/
public static function get($name)
{ if(isset(self::$objects[$name]))
{
return self::$objects[$name];
}
return false;
}
/**
* Removes an object out of Registry (Hardly used)
* @param string $name
* @return bool
*/
static function remove($name)
{
unset(self::$objects[$name]);
return true;
}
/**
* Checks if an object is stored within the registry
* @param string $name
* @return bool
*/
static function is_set($name)
{
return isset(self::$objects[$name]);
}
}`
The you can use like so.
require_once 'classes/registry.class.php';
require_once 'classes/database/pdo.class.php';
Registry::set('Database',new Database);
Then anywhere in your Application you can do like so:
function addPost(String $post,Int $id)
{
$statement = Registry::get('Database')->prepare('INSERT INTO posts VALUES(:d,:p)');
$statement->bindValue(':p',$id,PDO::PARAM_INT);
$statement->bindValue(':p',$post);
}
This will give you a much Larger scope!
精彩评论