L'estensione della classe PDO è probabilmente l'opzione migliore.
class MyPDO extends PDO
{
protected $_table_prefix;
protected $_table_suffix;
public function __construct($dsn, $user = null, $password = null, $driver_options = array(), $prefix = null, $suffix = null)
{
$this->_table_prefix = $prefix;
$this->_table_suffix = $suffix;
parent::__construct($dsn, $user, $password, $driver_options);
}
public function exec($statement)
{
$statement = $this->_tablePrefixSuffix($statement);
return parent::exec($statement);
}
public function prepare($statement, $driver_options = array())
{
$statement = $this->_tablePrefixSuffix($statement);
return parent::prepare($statement, $driver_options);
}
public function query($statement)
{
$statement = $this->_tablePrefixSuffix($statement);
$args = func_get_args();
if (count($args) > 1) {
return call_user_func_array(array($this, 'parent::query'), $args);
} else {
return parent::query($statement);
}
}
protected function _tablePrefixSuffix($statement)
{
return sprintf($statement, $this->_table_prefix, $this->_table_suffix);
}
}
fonte
2009-09-24 16:06:44
hi è solo a me o sulla funzione/metodo '_tablePrefixSuffix' l'ultimo bit' $ this_table_suffix' si è supponiamo di essere '$ this -> _ table_suffix'? – Val
Non sono sicuro di come questo possa essere considerato come un esempio funzionante, dal momento che 'sprintf' si aspetta stringhe di formato con ancoraggi speciali, ma l'argomento' $ statement' è solo una semplice stringa, che semplicemente viene ignorata e restituita così com'è. –
Ok, capito. Basta specificare il nome della tabella con le ancore: '$ table ="% 1 \ $ s {$ table}% 2 \ $ s "' –