Best way to integrate classes

This is a discussion on Best way to integrate classes within the PHP Language forums, part of the PHP Programming Forums category; Hi all. I'm working here on a small project of mine. I'm not new to programming, but I'...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-06-2004
Swartz
 
Posts: n/a
Default Best way to integrate classes

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


Reply With Quote
  #2 (permalink)  
Old 03-06-2004
Mike Peters
 
Posts: n/a
Default Re: Best way to integrate classes

On 2004-03-06, Swartz wrote:
> 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.
>

I'd go route 3. You can still access the session's DB object:

$my_session = new Session ();

$sql = 'SELECT blah FROM blah';

$my_session -> _oDB -> db_query ( $sql);

HTH
--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Reply With Quote
  #3 (permalink)  
Old 03-07-2004
jn
 
Posts: n/a
Default Re: Best way to integrate classes

"Mike Peters" <o0__mike__0oREMOVE@THIShotmail.com> wrote in message
news:88c3bebee110fa82039cd093a8ff742d@news.teranew s.com...
> On 2004-03-06, Swartz wrote:
> > 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.
> >

> I'd go route 3. You can still access the session's DB object:
>
> $my_session = new Session ();
>
> $sql = 'SELECT blah FROM blah';
>
> $my_session -> _oDB -> db_query ( $sql);
>
> HTH
> --
> Mike Peters
> mike [-AT-] ice2o [-DOT-] com
> http://www.ice2o.com
>


That's not a good practice though is it? Wasn't that object supposed to be
private to the session class?

By the way, I'm no expert on OOP :)


Reply With Quote
  #4 (permalink)  
Old 03-07-2004
Mike Peters
 
Posts: n/a
Default Re: Best way to integrate classes

On 2004-03-06, jn wrote:
> "Mike Peters" <o0__mike__0oREMOVE@THIShotmail.com> wrote in message
> news:88c3bebee110fa82039cd093a8ff742d@news.teranew s.com...
>> On 2004-03-06, Swartz wrote:
>> > 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.
>> >

>> I'd go route 3. You can still access the session's DB object:
>>
>> $my_session = new Session ();
>>
>> $sql = 'SELECT blah FROM blah';
>>
>> $my_session -> _oDB -> db_query ( $sql);
>>
>> HTH
>> --
>> Mike Peters
>> mike [-AT-] ice2o [-DOT-] com
>> http://www.ice2o.com
>>

>
> That's not a good practice though is it? Wasn't that object supposed to be
> private to the session class?
>
> By the way, I'm no expert on OOP :)
>

PHP4 has no concept of public / private, maybe the OP is using PHP5 but he
didn't say so. Anyway this is merely an example of containment - the
Session object 'has a' database object. We can access it directly like
above (as if we'd declared it public) or we could write a 'public' method
for our session class to access the 'private' object, the end result is the
same. I can see your point, and, in a compiled language I would agree, but
we're using php here so we also need to think about performance and the extra
overhead it would take to do it the 'right' way, at least in PHP4 where
the object model is less than perfect.
--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Reply With Quote
  #5 (permalink)  
Old 03-07-2004
Swartz
 
Posts: n/a
Default Re: Best way to integrate classes

Hi.

> > > 3.) Instantiate DB object as part of Session's vars:
> > >
> > > class Session {
> > > $_oDB = new DB();
> > >
> > > // login();
> > > // logout();
> > > // etc...
> > > }


I guess I better correct myself before somebody else does.
Unfortunatelly u cant have dynamic created values assigned to a class
variable.
Expression $_oDB = new DB() is invalid.

So the code should be the following:

class Session {
var $_oDB;

function Session () {
$this->_oDB = new DB();
}
// login, logout, etc

}

> > I'd go route 3. You can still access the session's DB object:
> > $my_session = new Session ();
> > $sql = 'SELECT blah FROM blah';
> > $my_session -> _oDB -> db_query ( $sql);

> That's not a good practice though is it? Wasn't that object supposed to be
> private to the session class?

Well, technically yes, it is not a good OOP. Mainly because class vars that
start with an underscore "_" are private vars by convention that many PHP
programmers use.

--
Swartz



Reply With Quote
  #6 (permalink)  
Old 03-07-2004
Mike Peters
 
Posts: n/a
Default Re: Best way to integrate classes

On 2004-03-07, Swartz wrote:
> Hi.
>
>> > > 3.) Instantiate DB object as part of Session's vars:
>> > >
>> > > class Session {
>> > > $_oDB = new DB();
>> > >
>> > > // login();
>> > > // logout();
>> > > // etc...
>> > > }

>
> I guess I better correct myself before somebody else does.
> Unfortunatelly u cant have dynamic created values assigned to a class
> variable.
> Expression $_oDB = new DB() is invalid.
>
> So the code should be the following:
>
> class Session {
> var $_oDB;
>
> function Session () {
> $this->_oDB = new DB();
> }
> // login, logout, etc
>
> }


I'd assumed your original post was merely shorthand and you'd meant this
anyway.

>
>> > I'd go route 3. You can still access the session's DB object:
>> > $my_session = new Session ();
>> > $sql = 'SELECT blah FROM blah';
>> > $my_session -> _oDB -> db_query ( $sql);

>> That's not a good practice though is it? Wasn't that object supposed to be
>> private to the session class?

> Well, technically yes, it is not a good OOP. Mainly because class vars that
> start with an underscore "_" are private vars by convention that many PHP
> programmers use.
>
> --
> Swartz
>
>
>



--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Reply With Quote
  #7 (permalink)  
Old 03-07-2004
Nikolai Chuvakhin
 
Posts: n/a
Default Re: Best way to integrate classes

"Swartz" <swartz@inbox.ru> wrote in message
news:<104khdbgrafe251@corp.supernews.com>...
>
> 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.


Actually, it doesn't seem "a little too objectified"; it seems
unnecessarily objectified. You are creating lots of overhead
here...

> What is the best approach to integrate the two classes?


The best approach is not to have classes at all, unless you
need them.

> How would you do it?


By writing standalone functions: login(), logout(), etc. Ask
yourself a simple question: will you ever have more than one
instance of your Session class running? Most likely, the answer
to that question is no, so you will do just fine with standalone
functions; they will get the job done and won't create any
unnecessary overhead...

> perhaps there're other way to do this that I'm not seeing.


Of course. Try to unlearn OOP, if you can.

Cheers,
NC
Reply With Quote
  #8 (permalink)  
Old 03-07-2004
Hayden Kirk
 
Posts: n/a
Default Re: Best way to integrate classes

Worst advice ever... im sorry.

I create classes for everything. Why? Because I might write a project in a
few months, instead of starting from scratch I can just include those
classes.

> Of course. Try to unlearn OOP, if you can.


Clearly you have never done much OOP.

Wait for php 5 and things are going to change.


"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32d7a63c.0403061840.51a9b9fa@posting.google.c om...
> "Swartz" <swartz@inbox.ru> wrote in message
> news:<104khdbgrafe251@corp.supernews.com>...
> >
> > 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.

>
> Actually, it doesn't seem "a little too objectified"; it seems
> unnecessarily objectified. You are creating lots of overhead
> here...
>
> > What is the best approach to integrate the two classes?

>
> The best approach is not to have classes at all, unless you
> need them.
>
> > How would you do it?

>
> By writing standalone functions: login(), logout(), etc. Ask
> yourself a simple question: will you ever have more than one
> instance of your Session class running? Most likely, the answer
> to that question is no, so you will do just fine with standalone
> functions; they will get the job done and won't create any
> unnecessary overhead...
>
> > perhaps there're other way to do this that I'm not seeing.

>
> Of course. Try to unlearn OOP, if you can.
>
> Cheers,
> NC



Reply With Quote
  #9 (permalink)  
Old 03-07-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: Best way to integrate classes

On 2004-03-06, Swartz <swartz@inbox.ru> wrote:
> 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.
>
> I see three possible solutions:
> 1.) Have the Session class extend the DB class?
> 2.) First, instantiate an object of DB:
> Then use it via global declaration in Session class
> 3.) Instantiate DB object as part of Session's vars:


I'd go for the 4th solution: An interface Session with functions login,
logout, etc...

Then implement this interface in a class DBSession and pass a reference
to the DB object as an argument in the constructor.

--
http://home.mysth.be/~timvw
Reply With Quote
  #10 (permalink)  
Old 03-07-2004
Bruno Desthuilliers
 
Posts: n/a
Default Re: Best way to integrate classes

Swartz wrote:
> 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?

(snip inheritence)
>
> 2.) First, instantiate an object of DB:
> $oDB = new DB();
>
> Then use it via global declaration in Session class

(snip use of a global)
>
> 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.


And what about :

class Session
{
var $_dbObject;

function Session(&$dbObject)
{
$this->dbObject =& $dbObject;
}
/* ... */
}

$db = new DB();
$session = new Session($db);

Easy enough, no ? I'd thought that <quote>coming from C++, OOP
world</quote>, you would at least know such a basic OO construction ?-)

Bruno

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:30 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0