impl = null; } function setImplementor(&$impl) { if (!is_object($impl) || !is_a($impl, 'Database')) return false; $this->impl =& $impl; return true; } function connect($host, $user, $pass, $base) { if (!$this->impl) return false; return $this->impl->connect($host, $user, $pass, $base); } function query($sql) { if (!$this->impl) return array(); return $this->impl->query($sql); } } Class Database { function connect($host, $user, $pass, $base) { return false; } function query($sql) { return array(); } } Class MySQL extends Database { var $_id; function connect($host, $user, $pass, $base) { $this->_id = @mysql_connect($host, $user, $pass); if (!$this->_id) return false; return @mysql_select_db($base, $this->_id); } function query($sql) { $tab = array(); $res = @mysql_query($sql, $this->_id); if (!$res) return array(); while ($row = @mysql_fetch_assoc($res)) $tab[] = $row; @mysql_free_result($res); return $tab; } } Class PostgreSQL extends Database { var $_id; function connect($host, $user, $pass, $base) { $this->_id = @pg_connect('host='.$host.' dbname='.$base.' user='.$user.' password='.$pass); if (!$this->_id) return false; return true; } function query($sql) { $tab = array(); $res = @pg_query($this->_id, $sql); if (!$res) return array(); while ($row = @pg_fetch_assoc($res)) $tab[] = $row; @pg_free_result($res); return $tab; } } $mgr =& new DatabaseManager(); $impl =& new MySQL(); $mgr->setImplementor($impl); echo $mgr->connect('localhost', 'root', '', 'mysql') ? 'Connecté !' : 'Non connecté !'; ?>