Hi all.
I'm working here on a small project of mine. I'm not new to programming, but
I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
so my code might seems a little too "object"-ified.
Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
for now, no error checking or anything. So now I'm trying to create
user/session-handling class that would work with the data stored in the
database via DB class.
What is the best approach to integrate the two classes? How would you do it?
I see three possible solutions:
1.) Have the Session class extend the DB class?
class Session extends DB {
// login();
// logout();
// etc...
}
This doesn't really make sense to me in terms of logical iheritance. Session
is not really a DB-handling class. Besides, I'm unclear on a few things
here. Will the constructor for DB class be called automatically and create a
connection (it should in my mind) or should I be re-initializing all
inherited vars?
2.) First, instantiate an object of DB:
$oDB = new DB();
Then use it via global declaration in Session class
class Session {
global $oDB;
// login();
// logout();
// etc...
}
Not the prettiest way, but it works. It would also allow to reuse the same
MySQL connection via $oDB object further in the code.
3.) Instantiate DB object as part of Session's vars:
class Session {
$_oDB = new DB();
// login();
// logout();
// etc...
}
The only drawback I can see is that I would be unable to reuse the MySQL
connection outside of Session class. I'd have to create a totaly new DB
object.
Oh and here's the DB class.
// +++++++++++++++ ++++ DB CLASS +++++++++++++++ ++
class DB {
// public variables
var $sServer;
var $sPort;
var $sUser;
var $sPass;
var $sDatabase;
// private variables
var $_link_id;
var $_result_id;
var $_last_query;
// Constructor, uses db_connect()
function DB ($user, $pass, $database, $server='localh ost', $port='3306') {
$this->connect($use r, $pass, $database, $server, $port);
}
// Opens a connection to a db-server and selects a db
function connect ($user, $pass, $database, $servet, $port) {
$this->sServer = $server;
$this->sPort = $port;
$this->sUser = $user;
$this->sPass = $pass;
$this->sDatabase = $database;
$this->_link_id = mysql_connect(" $server:$port", $user, $pass);
$this->select_db($dat abase, $this->_link_id);
}
// Closes connection to the database
function disconnect () {
$this->free_result( );
mysql_close($th is->_link_id);
unset ($this->_link_id, $this->_result_id);
}
// Changes database in use
function select_db ($database) {
mysql_select_db ($this->sDatabase, $this->_link_id);
}
// Run db query
function query ($query) {
$this->_last_query = $query;
$this->_result_id = mysql_query($qu ery, $this->_link_id);
}
// Frees up the memory required to store last query's results
function free_result () {
mysql_free_resu lt($this->_result_id);
unset ($this->_result_id);
}
// Returns a single record array from the query results
function fetch_row () {
return mysql_fetch_row ($this->_result_id);
}
// Returns a single record as associative array from query results
function fetch_assoc () {
return mysql_fetch_ass oc($this->_result_id);
}
// Returns a single record as object from query results
function fetch_object () {
return mysql_fetch_obj ect($this->_result_id);
}
// Returns number of records found after SELECT statement
function num_rows () {
return mysql_num_rows( $this->_result_id);
}
// Return number of rows affected from INSERT, UPDATE, DELETE statements
function affected_rows () {
return mysql_affected_ rows($this->_link_id);
}
// Returns current db link_id
function get_link_id () {
return $this->_link_id;
}
// Return current db result_id
function get_result_id () {
return $this->_result_id;
}
// Return last query ran
function get_last_query () {
return $this->_last_query;
}
// -----------------------------------------------------------------------
}
Any comments are welcome, perhaps there're other way to do this that I'm not
seeing.
--
Swartz
I'm working here on a small project of mine. I'm not new to programming, but
I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
so my code might seems a little too "object"-ified.
Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
for now, no error checking or anything. So now I'm trying to create
user/session-handling class that would work with the data stored in the
database via DB class.
What is the best approach to integrate the two classes? How would you do it?
I see three possible solutions:
1.) Have the Session class extend the DB class?
class Session extends DB {
// login();
// logout();
// etc...
}
This doesn't really make sense to me in terms of logical iheritance. Session
is not really a DB-handling class. Besides, I'm unclear on a few things
here. Will the constructor for DB class be called automatically and create a
connection (it should in my mind) or should I be re-initializing all
inherited vars?
2.) First, instantiate an object of DB:
$oDB = new DB();
Then use it via global declaration in Session class
class Session {
global $oDB;
// login();
// logout();
// etc...
}
Not the prettiest way, but it works. It would also allow to reuse the same
MySQL connection via $oDB object further in the code.
3.) Instantiate DB object as part of Session's vars:
class Session {
$_oDB = new DB();
// login();
// logout();
// etc...
}
The only drawback I can see is that I would be unable to reuse the MySQL
connection outside of Session class. I'd have to create a totaly new DB
object.
Oh and here's the DB class.
// +++++++++++++++ ++++ DB CLASS +++++++++++++++ ++
class DB {
// public variables
var $sServer;
var $sPort;
var $sUser;
var $sPass;
var $sDatabase;
// private variables
var $_link_id;
var $_result_id;
var $_last_query;
// Constructor, uses db_connect()
function DB ($user, $pass, $database, $server='localh ost', $port='3306') {
$this->connect($use r, $pass, $database, $server, $port);
}
// Opens a connection to a db-server and selects a db
function connect ($user, $pass, $database, $servet, $port) {
$this->sServer = $server;
$this->sPort = $port;
$this->sUser = $user;
$this->sPass = $pass;
$this->sDatabase = $database;
$this->_link_id = mysql_connect(" $server:$port", $user, $pass);
$this->select_db($dat abase, $this->_link_id);
}
// Closes connection to the database
function disconnect () {
$this->free_result( );
mysql_close($th is->_link_id);
unset ($this->_link_id, $this->_result_id);
}
// Changes database in use
function select_db ($database) {
mysql_select_db ($this->sDatabase, $this->_link_id);
}
// Run db query
function query ($query) {
$this->_last_query = $query;
$this->_result_id = mysql_query($qu ery, $this->_link_id);
}
// Frees up the memory required to store last query's results
function free_result () {
mysql_free_resu lt($this->_result_id);
unset ($this->_result_id);
}
// Returns a single record array from the query results
function fetch_row () {
return mysql_fetch_row ($this->_result_id);
}
// Returns a single record as associative array from query results
function fetch_assoc () {
return mysql_fetch_ass oc($this->_result_id);
}
// Returns a single record as object from query results
function fetch_object () {
return mysql_fetch_obj ect($this->_result_id);
}
// Returns number of records found after SELECT statement
function num_rows () {
return mysql_num_rows( $this->_result_id);
}
// Return number of rows affected from INSERT, UPDATE, DELETE statements
function affected_rows () {
return mysql_affected_ rows($this->_link_id);
}
// Returns current db link_id
function get_link_id () {
return $this->_link_id;
}
// Return current db result_id
function get_result_id () {
return $this->_result_id;
}
// Return last query ran
function get_last_query () {
return $this->_last_query;
}
// -----------------------------------------------------------------------
}
Any comments are welcome, perhaps there're other way to do this that I'm not
seeing.
--
Swartz
Comment