PHP Framework
Auteur: Raze - 06 april 2010 - 22:27 - Gekeurd door: Joel - Hits: 4199 - Aantal punten: 0.50 (2 stemmen)
LEES DE UPDATES BIJ EDIT 2!!
Dit is een basis framework in php. Het is gebaseerd op de volgende tutorials: http://net.tuts...ork-part-1/. Het is wel géén kopie, er zijn andere dingen aan toegevoegd (de echte basis is wel hetzelfde).
De opbouw van de mappen:
MAP /framework/
- framework.class.php
- exceptions.class.php
MAP /framework/objects/
- database.class.php
- validator.class.php
Het is vooral een voorbeeld, hoewel je er ook wel al een (basis) website mee kan bouwen. Je kan natuurlijk ook extra dingen toevoegen. De klasse validator is een voorbeeld: het heeft momenteel maar één functie (om een e-mailadres te controleren).
Het is echt een basis script, niet gelijk bv. zend framework (ZF is dan ook door meerdere mensen gebouwd).
EDIT (2010-04-09):
- Ik heb nu ook een postgresql versie gemaakt van de database klasse. De rest blijft hetzelfde.
EDIT 2 ( 2010-05-02):
- Het script is volledig veranderd, en is nu niet meer gebaseerd op de tutorials in de link, maar ik heb het nu zelf proberen te verzinnen.
- Er zit alleen nog maar een postgresql-versie in van de database-klasse, moest je graag een mysql-versie hebben kan je me steeds PM'en dan help ik je wel verder.
- De validator-klasse is verwijderd. Dit was toch slechts een voorbeeld.
- De mapstructuur:
/framework/
- framework.class.php
/objects/
- database.class.php
/exceptions/
- framework.exc.php
|
Code: |
Voorbeeld van gebruik:
<?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;
};
?>
<?php try { $fw = framework::initialize(); $key = $fw->useClass('db')->initConnection( "HOSTNAME" , "PORT" , "DATABASE" , "USERNAME" , "PASSWORD" ); $fw->useClass('db')->closeConnection( $key ); } catch( fw_exception $e ) { }; ?>
framework.class.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;
}
}
}
?>
<?php class framework { const FW_NAME = "Framework name"; const FW_VERSION = "1.0"; 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 () { { self::$instance = new self; } return self::$instance; } public function registerClass( $key , $classname ) { $file = "objects/".$classname.".class.php"; { 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 ) { { 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
<?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] );
}
}
}
?>
<?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 ) { { $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; { 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 ) { } /** * * @access public * @param integer $key */ public function numRows( $key ) { } /** * * @access public * @param integer $key */ public function fetchArray( $key ) { } /** * * @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; { 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; { 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 ) { { $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 ) { } /** * * @access public * @param integer $key */ public function closeConnection( $key = NULL ) { { return pg_close( $this->connections[$this->activeConnection] ); } else { return pg_close( $this->connections[$key] ); } } } ?>
framework.exc.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}";
}
}
?>
<?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}"; } } ?>
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|