Voorbeeld van gebruik:
[code=php]
<?php
try {	
	$fw = framework::initialize();

	$key = $fw->useClass('db')->initConnection( "HOSTNAME" , "PORT" , "DATABASE" , "USERNAME" , "PASSWORD" );
	$fw->useClass('db')->closeConnection( $key );
} 
	catch( fw_exception $e ) 
{ 
	echo $e; 
};
?>


framework.class.php
[code=php]
<?php

class framework
{
	const FW_NAME = "Framework name";
	
	const FW_VERSION = "1.0";
	
	public $data = array();
	
	public $objects = array();
	
	private static $instance = null;
	
	public function __construct()
	{
		require_once( "exceptions/framework.exc.php" );
	
		/**
		  * Register core classes
		  */
		$this->registerClass( "db" , "database" );
	}
	
	public function __clone()
	{
		throw new fw_exception( "Cloning is not allowed" );
	}
	
	public static function initialize()
	{
		if( is_null( self::$instance ) )
		{
			self::$instance = new self;
		}
		
		return self::$instance;
	}
	
	public function registerClass( $key , $classname )
	{
		$file = "objects/".$classname.".class.php";
		
		if( is_file( $file ) )
		{
			require_once( $file );
			$this->objects[$key] = new $classname;
		}
		else
		{
			throw new fw_exception( "Class not found" );
		}
	}
	
	public function useClass( $key )
	{
		return $this->objects[$key];
	}
	
	public function putData( $value )
	{
		$this->data[] = $value;
		
		return count($this->data)-1;
	}
	
	public function getData( $key )
	{
		if( is_numeric( $key ) )
		{
			return $this->data[$key];
		}
		else
		{
			throw new fw_exception( "Key value is not valid (not an integer)" );
		}
	}
	
	public function getFWdata( $datatype )
	{
		switch( $datatype )
		{
			case "FW_NAME":
				return self::FW_NAME;
			break;
			case "FW_VERSION":
				return self::FW_VERSION;
			break;
		}
	}
}

?>


database.class.php
[code=php]
<?php

/**
 *
 * Class database
 *
 *	public function initConnection( $hostname , $port , $database , $username , $password )
 *	public function setActiveConnection( $key )
 *	public function select( $items , $table , $condition = "" )
 *	public function escapeString( $input )
 *	public function numRows( $key )
 *	public function fetchArray( $key )
 *	public function insert( $table , $input )
 *	public function update( $table , $input , $condition )
 *	public function delete( $table , $condition )
 *	public function executeQuery( $querystring )
 *	public function affectedRows( $key )
 *	public function closeConnection( $key = NULL )
 */

class database
{
	public $connections = array();
	
	public $activeConnection;
	
	public $resultset = array();
	
	/**
	 *
	 * @access public
	 * @param string $hostname
	 * @param integer $port
	 * @param string $database
	 * @param string $username
	 * @param string $password
	 */
	public function initConnection( $hostname , $port , $database , $username , $password )
	{
		$connection_string = "host={$hostname} port={$port} dbname={$database} user={$username} password={$password}";
		
		if( ( $this->connections[] = pg_connect( $connection_string ) ) == true )
		{
			$connectionId = count($this->connections)-1;
			
			$this->activeConnection = $connectionId;
			
			return $connectionId;
		}
		else
		{
			throw new fw_exception( "Could not connect to the database" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param integer $key
	 */
	public function setActiveConnection( $key )
	{
		if( is_numeric( $key ) )
		{
			$this->activeConnection = $key;
		}
		else
		{
			throw new fw_exception( "Key not valid (not an integer)" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param array $items
	 * @param string $table
	 * @param string $condition
	 */
	public function select( $items , $table , $condition = "" )
	{
		$fields = NULL;
		
		if( is_array( $items ) && !empty( $items ) && !empty( $table ) )
		{
			foreach( $items as $value )
			{
				$fields .= "{$value},";
			}
			
			$fields = substr( $fields , 0 , -1 );
		
			if( !empty( $condition ) )
			{
				$condition = "WHERE {$condition}";
			}
			
			$querystring = "SELECT {$fields} FROM {$table} {$condition}"; 

			return $this->executeQuery( $querystring );			
		}
		else
		{
			throw new fw_exception( "Input data not valid" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param string $input
	 */
	public function escapeString( $input )
	{
		return pg_escape_string( $input );
	}
	
	/**
	 *
	 * @access public
	 * @param integer $key
	 */
	public function numRows( $key )
	{
		return pg_num_rows( $this->resultset[$key] );
	}

	/**
	 *
	 * @access public
	 * @param integer $key
	 */
	public function fetchArray( $key )
	{
		return pg_fetch_array( $this->resultset[$key] , NULL , PGSQL_ASSOC );	
	}
	
	/**
	 *
	 * @access public
	 */
	public function getDbname()
	{
		return pg_dbname( $this->connections[$this->activeConnection] );
	}
	
	/**
	 *
	 * @access public
	 */
	public function getPort()
	{
		return pg_port( $this->connections[$this->activeConnection] );
	}
	
	/**
	 *
	 * @access public
	 * @param string $table
	 * @param array $input
	 */
	public function insert( $table , $input )
	{
		$fields = NULL;
		$values = NULL;
		
		if( !empty( $table ) && is_array( $input ) && !empty( $input ) )
		{
			foreach( $input as $key=>$value )
			{
				$fields .= "{$key},";
				$values .= "'".self::escapeString( $value )."',";
			}
			
			$fields = substr( $fields , 0 , -1 );
			$values = substr( $values , 0 , -1 );
			
			$querystring = "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
			
			return $this->executeQuery( $querystring );
		
		}
		else
		{
			throw new fw_exception( "Input data not valid" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param string $table
	 * @param array $input
	 * @param string $condition
	 */
	public function update( $table , $input , $condition )
	{
		$updates = NULL;
		
		if( !empty( $table ) && is_array( $input ) && !empty( $input ) && !empty( $condition ) )
		{
			foreach( $input as $key=>$value )
			{
				$updates .= "{$key} = '{$value}',";
			}
			
			$updates = substr( $updates , 0 , -1 );
			
			$querystring = "UPDATE {$table} SET {$updates} WHERE {$condition}";
			
			return $this->executeQuery( $querystring );
		}
		else
		{
			throw new fw_exception( "Input data not valid" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param string $table
	 * @param string $condition
	 */
	public function delete( $table , $condition )
	{
		if( !empty( $table) && !empty( $condition ) )
		{
			$querystring = "DELETE FROM {$table} WHERE {$condition}";

			return $this->executeQuery( $querystring );
		}
		else
		{
			throw new fw_exception( "Input data not valid" );
		}
	}
	
	/**
	 *
	 * @access public
	 * @param string $querystring
	 */
	public function executeQuery( $querystring )
	{
		if( $result = pg_query( $this->connections[$this->activeConnection] , $querystring ) )
		{
			$this->resultset[] = $result;
			
			return count($this->resultset)-1;
		}
		else
		{
			throw new fw_exception( "Query failed" );
		}
	}

	/**
	 *
	 * @access public
	 * @param integer $key
	 */
	public function affectedRows( $key )
	{
		return pg_affected_rows( $this->resultset[$key] );
	}

	/**
	 *
	 * @access public
	 * @param integer $key
	 */
	public function closeConnection( $key = NULL )
	{
		if( is_null( $key ) )
		{
			return pg_close( $this->connections[$this->activeConnection] );
		}
		else
		{
			return pg_close( $this->connections[$key] );
		}
	}
}
?>


framework.exc.php
[code=php]
<?php

//////////////////////////////////
// EXCEPTIONS/FRAMEWORK.EXC.PHP //
//////////////////////////////////

class fw_exception extends exception
{
	public function __construct( $message , $code = 0 , Exception $previous = null )
	{
		parent::__construct( $message , $code , $previous );
	}
	
	public function __toString()
	{
		return __CLASS__.": [{$this->code}]: {$this->message} on line {$this->line}";
	}
}

?>
