Hi, I am in the process of making the switch from procedural to OO PHP. I'm just practising by setting up really simple config, database and auth classes. Before I end up going too far down the road with dodgy code, i would be interested to get some feedback on what i've done so far.
One thing I'm not sure about - where to start sessions, i.e in the auth class file in the constructor or outside of the class (but in the same file...if that makes sense).
Thanks in advance for any advice!
config.php
database.phpPHP Code:class Config {
protected $hostname = 'localhost';
protected $username = 'root';
protected $password = '';
protected $database = 'my_database';
/**
* Config::get_hostname()
*
* @return
*/
public function get_hostname()
{
return $this->hostname;
}
/**
* Config::get_username()
*
* @return
*/
public function get_username()
{
return $this->username;
}
/**
* Config::get_password()
*
* @return
*/
public function get_password()
{
return $this->password;
}
/**
* Config::get_database()
*
* @return
*/
public function get_database()
{
return $this->database;
}
}
auth.phpPHP Code:class Database
{
private $connectlink; //Database Connection Link
private $username;
private $password;
private $database;
private $hostname;
private $resultlink; //Database Result Recordset link
private $rows; //Stores the rows for the resultset
/**
* Database::__construct()
*
* @param mixed $conf
* @return
*/
public function __construct(Config $conf)
{
$this−>hostname = $conf->get_hostname();
$this−>username = $conf->get_username();
$this−>password = $conf->get_password();
$this−>database = $conf->get_database();
$this->connectlink = mysql_connect($this->hostname, $this->username, $this->
password);
if ( ! ($this->connectlink))
{
throw new Exception("Error Connecting to the Database");
} else
{
mysql_select_db($this->database);
}
}
/**
* Database::__destruct()
*
* @return
*/
public function __destruct()
{
@mysql_close($this->connectlink);
}
/**
* Database::query()
*
* @param mixed $sql
* @return
*/
public function query($sql)
{
$this->resultlink = mysql_query($sql);
return $this->resultlink;
}
/**
* Database::fetch_rows()
*
* @param mixed $result
* @return
*/
public function fetch_rows($result)
{
$rows = array();
if ($result)
{
while ($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
} else
{
throw new Exception("Error Retrieving Records");
$rows = null;
}
return $rows;
}
}
PHP Code:include("database.php");
class Auth {
protected $db;
/**
* Auth::__construct()
*
* @return
*/
public function __construct()
{
$db = new Database;
}
//function to check login credentials
/**
* Auth::login()
*
* @param mixed $username
* @param mixed $password
* @return
*/
public function login($username, $password)
{
$query = $db->query("SELECT username, password FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = sha1('".mysql_real_escape_string($password)."')");
$result = $db->fetch_rows($query);
if (count($result) == 0)
{
return FALSE;
}
else
{
//set session variable
$_SESSION['logged_in'] = TRUE;
return TRUE;
}
}
//logout function
/**
* Auth::logout()
*
* @return
*/
public function logout()
{
return $_SESSION['logged_in'] = FALSE;
}
}








Bookmarks