te estas enfrentando a este problema por que no has separado bien las responsabilidades de cada clase. Crea tu propia clase para base de datos y utiliza un patrón de diseño conocido como "Singleton" para obtener solo 1 conexión por base de datos. A continuación te dejo los inicios de una nueva clase que he iniciado a utilizar para mis conexiones.
Código PHP:
<?php
/**
* @package DbUtils
*/
/**
* this class is a small abstraction of the PDO (PHP Dataobjects) database model
* it implements singleton pattern to ensure that just 1 instance is created for
* each requested database (DSN)
* @todo hum, i guess a lot. I have only implemented what i need for now
* @final
*/
final class PdoDatabase
{
/**
* array object of intances of PdoDatabase objects.
* Only one object for DSN is allowed. So we
* have an array object
* @see PdoDatabase::GetPdoDatabase
* @var object PdoDatabase
* @static
* @access private
*/
private static $databases = NULL;
/**
* @var object PDO
* @access private
*/
private $pdo_object = NULL;
/**
* whether or not show debug info to STDOUT
* @var bool
* @access private
*/
private $debug_mode = FALSE;
/**
* PdoDatabase private constructor. Just to ensure that no fool can
* instanciate this class we declare a dummy private constructor
* @param void
* @access public
*/
private function __construct(){}
/**
* set the PDO object to use with this PdoDatabase
* @return void
* @param object PDO $PdoObject
* @access private
*/
private function SetPdoObject(PDO $PdoObject)
{
$this->pdo_object = $PdoObject;
}
/**
* return the PDO object reference used by this PdoDatabase
* @return object PDO
* @param void
* @access public
*/
public function GetPdoReference()
{
return $this->pdo_object;
}
/**
* Turn debug mode on. It will stay on during all the script execution
* @return void
* @param void
* @access public
*/
public function TurnOnDebugMode()
{
$this->debug_mode = TRUE;
$this->pdo_object->setAttribute(PDO::ATTR_ERRMODE, PDO_ERRMODE_WARNING);
}
/**
* @return PdoDatabase Object. It ensure the existance of
* only 1 object for each DSN
* @param string $PdoDsn
* @static
* @access public
* @throw PDOException
*/
public static function GetPdoDatabase($PdoDsn)
{
/* if is the first database connection to open, init the arrayobject of databases */
if ( self::$databases === NULL )
{
self::$databases = new ArrayObject();
}
try
{
/* if a connection to the provided PdoDsn already exists, return it */
if ( self::$databases->offsetExists($PdoDsn) )
{
return $databases->offsetGet($PdoDsn);
}
else
{
/* otherwise open the new connection, add it to databases and return it */
$new_pdo = new PDO($PdoDsn);
$new_db = new PdoDatabase();
$new_db->SetPdoObject($new_pdo);
$new_db->SetAttribute(PDO::ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
self::$databases->offsetSet($PdoDsn, $new_db);
return $new_db;
}
}
catch ( PDOException $PdoException )
{
/* oops, something has gone wrong, just re-throw the PDOException */
throw $PdoException;
}
}
/**
* prepares and executes the provided statement $Statement
* with the given statement values $StatementValues
* @return void
* @param string $Statement
* @param array $StatementValues
* @access public
* @throw PDOException
*/
public function ExecuteStatement($Statement, array $StatementValues)
{
/* check debug mode, if so, dump the arguments */
if ( $this->debug_mode )
{
echo 'Executing statement ' . $Statement . "\n";
var_dump($StatementValues);
}
/* if we have a previous statement, clear possible buffer data */
if ( $this->pdo_statement instanceof PDOStatement )
{
$this->pdo_statement->closeCursor();
}
/* execute the query and save the result in internal statement */
$this->pdo_statement = $this->pdo_object->prepare($Statement);
$this->pdo_statement->execute($StatementValues);
}
/**
* executes again the last executed statement with the given
* statement values $StatementValues
* @return void
* @param array $StatementValues
* @access public
*/
public function ExecuteLastStatement(array $StatementValues)
{
/* if we dont have a previous valid statement, throw exception */
if ( !($this->pdo_statement instanceof PDOStatement) )
{
throw BadFunctionCallException('You must execute a statement before calling ' . __METHOD__);
}
$this->pdo_statement->execute($StatementValues);
}
/**
* return a row data from the last statement executed
* @return void
* @param enum $FetchMode
* @access public
*/
public function FetchStatementData($FetchMode = PDO::FETCH_LAZY)
{
if ( !$this->pdo_statement->setFetchMode($FetchMode) )
{
throw new InvalidArgumentException("it seems that '{$FetchMode}' is not a valid FetchMode");
}
if ( $this->pdo_statement instanceof PDOStatement )
{
if ( ( $db_row = $this->pdo_statement->fetch() ) !== FALSE )
{
return $db_row;
}
}
else
{
throw new BadFunctionCallException('a valid statement must exists to call ' . __METHOD__);
}
return NULL;
}
}
?>