Ein Fehler vom Autor vielleicht?
Papier ist geduldig.
Papier ist geduldig.
<?php
$con = new MySQLConnection('localhost', 'root', '', 'test');
$Query1 = new MySQLQuery($con);
$Query2 = new MySQLQuery(new MySQLPeristentConnection('localhost', 'root', '', 'test'));
$Query3 = new MySQLQuery(new MySQLConnection('throw_an_exception', 'root', '', 'test'));
abstract class MySQLConnectionBase
{
protected $connection = NULL;
final public function getConnection()
{
if (!$this->connection)
throw new MySQLNotConnected();
return $this->connection;
}
final public function __construct($host, $user, $pass, $database, $flags = NULL, $port = 3306)
{
$this->connect($host, $user, $pass, $flags, $port);
$this->setDatabase($database);
}
abstract public function connect($host, $user, $pass, $flags = NULL, $port = 3306);
public function setDatabase($dbname)
{
if (!$this->connection)
throw new MySQLNotConnected();
if (!@mysql_select_db($dbname, $this->connection))
throw new MySQLCannotSelectDatabase();
}
}
class MySQLConnection extends MySQLConnectionBase
{
public function connect($host, $user, $pass, $flags = NULL, $port = 3306)
{
if ($this->connection)
$newConn = FALSE;
else
$newConn = TRUE;
$this->connection = @mysql_connect ( $host.":".$port , $user, $pass, $newConn, $flags);
if (!$this->connection)
throw new MySQLNotConnected($this);
}
}
class MySQLPeristentConnection extends MySQLConnectionBase
{
public function connect($host, $user, $pass, $flags = NULL, $port = 3306)
{
$this->connection = @mysql_pconnect ( $host.":".$port , $user, $pass, $flags);
if (!$this->connection)
throw new MySQLNotConnected($this);
}
}
class MySQLQuery
{
protected $CON = NULL;
public function __construct(MySQLConnectionBase $connection)
{
$this->CON = $connection;
}
public function execute($sql)
{
}
public function prepare()
{
}
}
class MySQL_Exception_Base extends Exception
{
public function __construct($param = NULL)
{
if (is_array($param))
{
if (isset($param['error']) AND isset($param['errno']))
parent::__construct($param['error'], $param['errno']);
if (isset($param['error']) AND !isset($param['errno']));
parent::__construct($param['error']);
if (!isset($param['error']) AND isset($param['errno']));
parent::__construct('', $param['errno']);
}
elseif ($param == NULL)
{
parent::__construct();
}
elseif ($param instanceof MySQLConnectionBase)
{
$error = mysql_error($param->getConnection());
$errno = mysql_errno($param->getConnection());
parent::__construct($error, $errno);
}
else
{
trigger_error("param must be an array or a MySQLConnectionBase instance", E_USER_ERROR);
}
}
}
class MySQLNotConnected extends MySQL_Exception_Base {}
class MySQLCannotSelectDatabase extends MySQL_Exception_Base {}
?>
<?php
...
$Query2 = new MySQLQuery(new MySQLPeristentConnection('localhost', 'root', '', 'test'));
...
?>
<?php
...
doSomething(new FooFactoryEven);
...
?>
class Factory {
...
public function getConnection() {
if( bla ) return new MySQLPeristentConnection('localhost', 'root', '', 'test');
if(blu) return new MySQLConnection('throw_an_exception', 'root', '', 'test');
}
......
}
// usage:
$factory = new Factory();
$con = $factory -> getConnection();
$Query1 = new MySQLQuery($con);
$Query2 = new MySQLQuery($factory -> getConnection());
$Query3 = new MySQLQuery($factory -> getConnection());
Kommentar