* $db = ezcDbFactory::create( $dbparams ); * ezcDbInstance::set( $db ); * * // ... * * $db = ezcDbInstance::get(); * * * More complex example, with two connections, having identifiers (for convenience): * * $mydb = ezcDbFactory::create( $mysql_dbparams ); * $pgdb = ezcDbFactory::create( $pgsql_dbparams ); * * ezcDbInstance::set( $mydb, 'my' ); * ezcDbInstance::set( $pgdb, 'pg' ); * ezcDbInstance::chooseDefault( 'my' ); * * // ... * * $mydb = ezcDbInstance::get( 'my' ); // returns the mysql instance * $pgdb = ezcDbInstance::get( 'pg' ); // returns the pgsql instance * $mydb = ezcDbInstance::get(); // returns the mysql instance which is default * * * @package Database */ class ezcDbInstance { /** * Identifier of the instance that will be returned * when you call get() without arguments. * * @see ezcDbInstance::get() */ static private $DefaultInstanceIdentifier = false; /** * Example: * * array( 'mysql1' => [object], * 'mysql2' => [object], * 'oracle' => [object] ) * */ static private $Instances = array(); /** * Return instance by its identifier. * * If identifier is not specified and default instance is set * by chooseDefault() then the default instance is returned. * * @param string $identifier Identifier of the instance to get. * @returns ezcDbHandler requested instance. * @throws ezcDbException::INSTANCE_NOT_FOUND if specified isnstance is not found. */ public static function get( $identifier = false ) { if ( $identifier === false && self::$DefaultInstanceIdentifier ) { $identifier = self::$DefaultInstanceIdentifier; } if ( !isset( self::$Instances[$identifier] ) ) { throw new ezcDbException( ezcDbException::INSTANCE_NOT_FOUND ); } return self::$Instances[$identifier]; } /** * Save specified instance, assigning it an indentifier (if specified). */ public static function set( $db, $identifier = false ) { self::$Instances[$identifier] = $db; } /** * Choose the instance that will be returned * if identifier is omitted when calling get(). * * @see ezcDbInstance::get(). */ public static function chooseDefault( $identifier ) { self::$DefaultInstanceIdentifier = $identifier; } /** * Reset default instance. */ public static function resetDefault() { self::$DefaultInstanceIdentifier = false; } /** * Reset object, returning it to the initial state. * * The list of instances will be emptied, * {@link resetDefault()} will be called. * * Is this method useful? */ public static function reset() { self::$Instances = array(); self::resetDefault(); } } ?>