PHP Classes

Good use of class separation (the mysql and query class divid...

Recommend this page to a friend!

      MySQL connection and query  >  All threads  >  Good use of class separation (the...  >  (Un) Subscribe thread alerts  
Subject:Good use of class separation (the...
Summary:Package rating comment
Messages:4
Author:Jonathan Dequeker
Date:2010-05-25 17:16:00
Update:2010-06-14 09:02:00
 

Jonathan Dequeker rated this package as follows:

Utility: Sufficient
Consistency: Good

  1. Good use of class separation (the...   Reply   Report abuse  
Picture of Jonathan Dequeker Jonathan Dequeker - 2010-05-25 17:16:00
Good use of class separation (the mysql and query class divided),
but the methods should have a scope declared (public, private, protected)
and I'd prefer to have it all made static, so i wouldn't have to call an object,
or create a better way of using the objects (please remove the $db from the file,
use a static class for settings)

  2. Re: Good use of class separation (the...   Reply   Report abuse  
Picture of Hansen Wong Hansen Wong - 2010-05-28 18:39:18 - In reply to message 1 from Jonathan Dequeker
Thanks for your sugestion... I have been moved variable $db.

  3. Re: Good use of class separation (the...   Reply   Report abuse  
Picture of Jonathan Dequeker Jonathan Dequeker - 2010-05-29 10:51:56 - In reply to message 1 from Jonathan Dequeker
I made the following of the mysql class:
<?php
/**
* Database class
*
* Establishes a database connection
*
* @author Huang_hanzen <[email protected]>
* @edited Jonathan Dequeker <[email protected]>
*/
class database {

/**
* @var string Name of database host
*/
private $host = "localhost";

/**
* @var string
*/
private $username;

/**
* @var string
*/
private $password;

/**
* @var string Database name
*/
private $database;

/**
* @var object Must be instance of PDO
*/
private $connection;

/**
* @var array List of occured errors
*/
public $errors = array();

/**
* set data if given, connect if possible
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @param boolean $warn Set to true to throw error on non-connection
* @return bool
*/
public function __construct($host, $username, $password, $database, $warn = false)
{
$this->setData($host, $username, $password, $database);

try{

$connected = $this->connect();

}catch(Exception $e) {

return false;

}

return $connected;
}

/**
* connect to MySQL database
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @return bool
*/
public function connect($host = NULL, $username = NULL, $password = NULL, $database = NULL)
{
$this->setData($host, $username, $password, $database);

try{

if(!isSet($this->host) or !is_string($this->host)) { throw new Exception("Host name not set."); }
if(!isSet($this->username) or !is_string($this->username)) { throw new Exception("Username not set."); }
if(!isSet($this->password) or !is_string($this->password)) { throw new Exception("Password not set."); }
if(!isSet($this->database) or !is_string($this->database)) { throw new Exception("Database name not set."); }

$this->connection = new PDO('mysql:host=localhost;dbname='.$this->database, $this->username, $this->password);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}catch(PDOException $e) {

$this->error($e->getMessage());
return false;

}catch(Exception $e) {

$this->error($e->getMessage());
return false;

}

return true;
}

/**
* set database configurations
* @param string $host
* @param string $username
* @param string $password
* @param string $database
*/
public function setData($host = NULL, $username = NULL, $password = NULL, $database = NULL)
{
if(isSet($host) and is_string($host)) { $this->host = $host; }
if(isSet($username) and is_string($username)) { $this->username = $username; }
if(isSet($password) and is_string($password)) { $this->password = $password; }
if(isSet($database) and is_string($database)) { $this->database = $database; }
}

/**
* throws a databaseError or logs error
* @param string $message
* @param integer $code
* @return string If the error was logged, the error key
*/
public function error($message = NULL, $code = NULL)
{
if($this->exceptions) {

throw new databaseError($message,$code);
return;

}else{

$this->errors[date("H:i:s",time())] = array($code => $message);
return date("H:i:s",time());

}
}

/**
* get last error
* @param string $message
* @param integer $code
* @return string
*/
public function getError()
{
if($last = end((array) $this->errors)) {

return $last[0];

}else{

return false;

}
}

/**
* unset the connection
*/
public function __destruct()
{
$this->connection = NULL;
}

}
?>

  4. Re: Good use of class separation (the...   Reply   Report abuse  
Picture of Hansen Wong Hansen Wong - 2010-06-14 09:02:00 - In reply to message 3 from Jonathan Dequeker
Thanks Jonathan. I very like your class function but when i test, it have a little error in:
"public function getError(){
if($last = end((array)$this->errors)){
return $last[0];
}
else{
return false;
}
}
"
i think the problem at "end((array)$this->errors)".

thanks for your help.