Best way to integrate classes

From: Swartz (swartz_at_inbox.ru)
Date: 03/06/04


Date: Sat, 6 Mar 2004 15:52:41 -0600

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='localhost', $port='3306') {
  $this->connect($user, $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($database, $this->_link_id);
 }

 // Closes connection to the database
 function disconnect () {
  $this->free_result();
  mysql_close($this->_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($query, $this->_link_id);
 }

 // Frees up the memory required to store last query's results
function free_result () {
  mysql_free_result($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_assoc($this->_result_id);
 }

 // Returns a single record as object from query results
 function fetch_object () {
  return mysql_fetch_object($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


Relevant Pages

  • OleDBConnection problem if database sesison is killed or database is restarted
    ... We are facing problem for OleDBConnection if my connection to database ... System.Data.OleDb.OleDbException: Session closed/terminated ... For executing SQL query we create new OleDBConnection, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Update checking
    ... > scenario where you'd want to use just one connection shared across all ... > query will be completely isolated from what you are doing. ... > end,closing a connection in one session will have no effect on any one ... > have a problem with process A cancelling process B's connection or vice ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Managing OLAP Connections
    ... don't close the session, close the connection but not the session. ... save the session ID to reuse it when the user ask the cube again. ... Try to find some solution to use the connection pooling feature. ... query I create the connection, make the query and then close the connection. ...
    (microsoft.public.sqlserver.olap)
  • Re: Finally which ORM tool?
    ... the session' method. ... they use the same mechanism as Linq to Sql does: ... Also, if you pass a variable to the query, the value the variable ... q is affected if I change foo AFTER this query and BEFORE execution. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: APPN Connection z/os - AHHC / LLC2.
    ... implied that you session stopped for a while and then continued. ... the ANNC link is a type 2.1 connection. ... I would suggest trying to limit the request unit size used over the type 2.1 ... MAXDATA operand when defining a PU statement for an adjacent link station ...
    (bit.listserv.ibm-main)