473,657 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='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
Jul 17 '05 #1
35 2646
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
Jul 17 '05 #2
jn
"Mike Peters" <o0************ ****@THIShotmai l.com> wrote in message
news:88******** *************** *******@news.te ranews.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 :)
Jul 17 '05 #3
On 2004-03-06, jn wrote:
"Mike Peters" <o0************ ****@THIShotmai l.com> wrote in message
news:88******** *************** *******@news.te ranews.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
Jul 17 '05 #4
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

Jul 17 '05 #5
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
Jul 17 '05 #6
"Swartz" <sw****@inbox.r u> wrote in message
news:<10******* ******@corp.sup ernews.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
Jul 17 '05 #7
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:32******** *************** ***@posting.goo gle.com... "Swartz" <sw****@inbox.r u> wrote in message
news:<10******* ******@corp.sup ernews.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

Jul 17 '05 #8
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(&$dbObj ect)
{
$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

Jul 17 '05 #9
Nikolai Chuvakhin wrote:
"Swartz" <sw****@inbox.r u> wrote in message
news:<10******* ******@corp.sup ernews.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...


All your rant against OO could be justified if PHP had any other support
for modularity. Since this is not the case, and there's no way to have
functions or vars existing on a 'module' scope only, the only way to
write clean PHP code is to use OO.

The "unnecessar y overhead" is the price to pay to have clean, modular
code with lo coupling, and as such is *never* "unnecessar y" except
perhaps for the most basic, throw-away scripts.
Bruno

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1840
by: eric | last post by:
Hi, I would like to build the inParams SQLJ sample procedure from the Development Center. if I declare a #sql public iterator SpIterat, the Development Center complains this should be in a separate file. if I declare a #sql iterator SpIterat, the Development Center complains the statements #sql updateIterator are not Java ...
1
4122
by: Johnny Meredith | last post by:
Dear All, I have an Access database that tracks the progress of income tax audits. When the taxing authorities make a change (an "Adjustment"), I record the pertinent information in the database. At the end of the audit cycle, these adjustments are used to compute the revised taxes due. This computation is done in a fairly complex, but accurate and easy to use, spreadsheet. In the spreadsheet, there is a sheet that lists out all...
0
1229
by: BFM | last post by:
Hello, I'm just beginning a project, and looking through much documentation, and trying to figure out the best platform to develop the core of my project in/on. The project will consist of a server/client relationship, with both the server and client needing access to much of the Windows API. It will need to integrate with Outlook, and probably access to other selected Window programs.
4
1507
by: Kjell Arne Johansen | last post by:
Hi How to integrate an MFC 7.0 DLL into a standard C++ application. I'm going to use an OPC Toolkit for C++ and have to interface my MFC DLLs. How do I do this? Any recommendations? Links? Regards Kjell Arne Johansen
0
1138
by: P McGinness | last post by:
Basically, I've a JS function called ValidateForm(), which returns true or false and controls if the form submits. When I integrate this with .NET validators, the .NET always adds it's own call to a function to the end of the form onSubmit script. So if my function returns false, .NETs function is still called and when it returns true will basically render my code pointless. I'm not entirely sure of the best way to integrate custom
3
5489
by: hwang2404 | last post by:
I have PHP 5.0.5 and java 1.2.2. I am trying to integrate them together. I have configure the php.ini as following: ;This must point to the location of php_java.dll. java.library.path = "C:\php\ext" ; This points to the directory where your Java ; classes will be stored. You can designate multiple ; paths, each path separated by a semicolon. ; It must also include the location of php_java.jar java.classpath =...
8
2770
by: Phil Latio | last post by:
I've been creating an application over the last few weeks and generally pleased with what I have produced but one area is irritating me, form validation. At the moment the forms are simply static html templates and the form input is checked using a validation class. Basically each form field is checked, every error is stored to an array and at the end of checking of the complete form, the array is output neatly at the top of the form. ...
5
1865
by: Allan Ebdrup | last post by:
I have a webservice in ASP.Net 2.0, I need to store and load a xml configuration file in relation to one of the web methods in my webservice. What's the best place to store and the best way to load my xml configuration file? I can't integrate the settings in web.config, it has to be a seperate file. Kind Regards, Allan Ebdrup
8
1836
by: raaman rai | last post by:
i am adding dynamic contents in my website. So i also want to have a news or announcements to be dynamically displayed. But for this i need to use BBCode. But in reference to this, i dont know how the BBCode parser should be configured. Is it to be configured by ourselves or will it be done by the hosting ISP. And will the tag be supported by BBCode?
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.