05-20-2009, 10:04 AM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 170
Thanks: 18
|
What I did now instead of the global defined LOCAL variable, I made a Registry class where I save global variables. Then i pass the Registry class to every other class which might need the global variables.
In my init.php file:
php Code:
// Load the registry $pRegistry = new Registry();
// Check if we are local if ($_SERVER['SERVER_ADDR'] == '127.0.0.1') { $pRegistry->is_local = TRUE; } else { $pRegistry->is_local = FALSE; }
// Load database with the Registry settings $pRegistry->m_pDatabase = Database::get_instance($pRegistry);
And then my Database class looks like:
php Code:
<?phpclass Database { private static $m_pInstance; private $m_pConn; private $m_pRegistry; private function __construct(Registry $pRegistry) { $this-> m_pRegistry = $pRegistry; if ($this-> m_pRegistry-> is_local) { $this-> connect('hostname', 'username', 'password', 'database'); } else { $this-> connect('hostname', 'username', 'password', 'database'); } } private function __clone() { } public static function get_instance (Registry $pRegistry = NULL) { if (!self:: $m_pInstance) { self:: $m_pInstance = new Database ($pRegistry); } return self:: $m_pInstance; } private function connect ($szHostname, $szUsername, $szPassword, $szDatabase) { try { $this-> m_pConn = new PDO ('mysql:host='. $szHostname. ';dbname='. $szDatabase, $szUsername, $szPassword); if ($this-> m_pRegistry-> is_local) { $this-> m_pConn-> setAttribute(PDO:: ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION); } else { $this-> m_pConn-> setAttribute(PDO:: ATTR_ERRMODE, PDO:: ERRMODE_SILENT); } } catch (PDOException $e) { echo $e-> getMessage(); } } public function query ($szQuery) { $pStatement = $this-> m_pConn-> prepare($szQuery); $pStatement-> execute(); $aResult = $pStatement-> fetchAll(); return $aResult; } public function select_query ($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $szQuery = 'SELECT '; $szQuery .= implode(', ', $aFields); $szQuery .= ' FROM '. $szTable; if (is_array($aWhere)) { $szWhere = ''; foreach($aWhere as $szKey => $szValue) { if (! is_numeric($szValue)) { $szValue = "'". $szValue. "'"; } $szWhere .= ' '. $szKey. ' = '. $szValue. ' AND'; } $szWhere = rtrim($szWhere, 'AND'); $szQuery .= ' WHERE '. $szWhere; } if (is_array($aOrderBy)) { $szOrderBy = ''; foreach($aOrderBy as $szKey => $szValue) { $szOrderBy .= ' '. $szKey. ' '. $szValue. ','; } $szOrderBy = rtrim($szOrderBy, ','); $szQuery .= ' ORDER BY '. $szOrderBy; } if ($aLimit !== NULL) { $szLimit = implode(',', $aLimit); $szQuery .= ' LIMIT '. $szLimit; } return $this-> query($szQuery); } public function select_query_row ($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $aResult = $this-> select_query($szTable, $aFields, $aWhere, $aOrderBy, $aLimit); return (isset($aResult[ 0] ) ? $aResult[ 0] : FALSE); } public function select_query_column ($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $aResult = $this-> select_query_row($szTable, $aFields, $aWhere, $aOrderBy, $aLimit); return (isset($aResult[ 0] ) ? $aResult[ 0] : FALSE); } public function insert_query ($szTable, $aFields, $aValues) { $szQuery = 'INSERT INTO '. $szTable. ' ('; $szQuery .= implode(', ', $aFields); $szQuery .= ') VALUES'; foreach ($aValues as $aValue) { $aValue = array_map(array($this, 'clean_input'), $aValue); $szQuery .= ' ('; $szQuery .= implode(', ', $aValue); $szQuery = rtrim($szQuery, ','); $szQuery .= '),'; } $szQuery = rtrim($szQuery, ','); $iCount = $this-> m_pConn-> exec($szQuery); return $iCount; } private static function clean_input ($mInput) { if (is_numeric($mInput)) { return $mInput; } else { return "'". $mInput. "'"; } }}?>
Any tips/suggestions on this way of doing things?
|
|
|
|