OOP guidance...
Posted: Wed Jan 18, 2006 2:45 pm
OK... i'm playing bit with OOP/PHP5. I got working what I needed but would like some feedback on what I've done.
2. What's the best way to go about constructing the page. Should I just go with HTML and call methods where needed in my page or should I go with Page object and have its methods output HTML. I know that it'll probably come to preferences but would like to hear from your experience.
Regards,
wtf
Code: Select all
class DBConnectionManager {
private $m_host;
private $m_user;
private $m_password;
private $m_database;
private $m_link;
public function __construct($host, $user, $password, $database) {
$this->m_host = $host;
$this->m_user = $user;
$this->m_password = $password;
$this->m_database = $database;
$this->dbConnect();
}
public function dbConnect() {
echo '<b>DBConnectionManager->dbConnect()</b> <br />';
try {
$this->m_link = mysql_connect($this->m_host, $this->m_user, $this->m_password);
if(!$this->m_link) {
throw new Exception('Host not available');
}
if(!mysql_select_db($this->m_database, $this->m_link)) {
throw new Exception('Database not available');
}
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
return $this->m_link;
}
} // End class DBConnectionManagerCode: Select all
require_once('classes/DBConnectionManager.php');
class SQLHelper extends DBConnectionManager {
private $m_dbm;
private $m_result;
private $m_row;
function __construct() {
parent::__construct('localhost', 'user', 'pass', 'database');
}
function query($query) {
echo '<b>SQLHelper->query("' . $query . '")</b> <br />';
try {
$this->m_result = mysql_query($query);
if(!$this->m_result) {
throw new Exception('your query sucks');
}
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
return $this->m_result;
}
function num_rows($result) {
echo '<b>SQLHelper->num_rows()</b> <br />';
return mysql_num_rows($result);
}
function queryToArray($result) {
while($myrow = mysql_fetch_array($result, MYSQL_NUM)) {
$myr[] = $myrow;
}
return $myr;
}
function arrayTo_TR($array) {
for($i = 0; $i < count($array); $i++) {
$this->m_row .= '<tr><td>' . $array[$i][0] . '</td><td>' . $array[$i][3] . '</td></tr>';
}
return $this->m_row;
}
}Code: Select all
require_once('classes/SQLHelper.php');
$sql = new SQLHelper();
$result = $sql->query('select * from table');
echo '<table border="1">';
echo $sql->arrayTo_TR($sql->queryToArray($result));
echo '</table>';Regards,
wtf