473,513 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple example of how to use a db access class

Hi,

I'm building a simple Object Oriented CMS using PHP5
and the new object model (new to both). I'm looking
for some design advice on how best to integrate the
database class - which creates the db connection and
executes the sql queries. In my particular case,
I have 3 objects (classes) that I instantiate from
a given script (call it index.php). Each must have
access to the database. So, do I create a db connection
once and declare it 'global' in the various classes?
or do I create a db connection in each class - maybe
in the constructor? or is there some other practice
that's widely accepted? Any recommendations are
greatly appreciated.

<?php
// index.php
$dbconn = csite::DB();

$a = new cvisitor();
$b = new cvalid8r();
$c = new cnode();

... blah blah
?>

<?php
// cvisitor.php
class cvisitor {
public function __construct() {
}
public function __destruct() {
}

public function getVisitor() {
// open the db
global $dbconn;

... do the db stuff
?>

Thanks,
b

Nov 22 '05 #1
1 1854
Anonymous via the Cypherpunks Tonga Remailer wrote:
Hi,

I'm building a simple Object Oriented CMS using PHP5
and the new object model (new to both). I'm looking
for some design advice on how best to integrate the
database class - which creates the db connection and
executes the sql queries. In my particular case,
I have 3 objects (classes) that I instantiate from
a given script (call it index.php). Each must have
access to the database. So, do I create a db connection
once and declare it 'global' in the various classes?
or do I create a db connection in each class - maybe
in the constructor? or is there some other practice
that's widely accepted? Any recommendations are
greatly appreciated.
Don't use global variables if you can help it. They create runtime
dependencies in your code, making your code less reuseable and harder
to understand/debug.

A general technique I use for such situations is to call a function to
retrieve the database connection. The function itself would use a
static variable to cache the handler, so that it wouldn't need to go
through the same motion when it's called again:

function GetDatabaseConnection() {
static $connection;
if(is_null($connection)) {

/* open the connection */
$connection = ???

}
return $connection;
}

Another way to model it is to pass the database connection in each
call:

getVisitor($dbconn);

That gives your more flexibility, since your code could potentially
operate on different databases.

<?php
// index.php
$dbconn = csite::DB();

$a = new cvisitor();
$b = new cvalid8r();
$c = new cnode();

... blah blah
?>

<?php
// cvisitor.php
class cvisitor {
public function __construct() {
}
public function __destruct() {
}

public function getVisitor() {
// open the db
global $dbconn;

... do the db stuff
?>

Thanks,
b


Nov 22 '05 #2

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

Similar topics

3
3663
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
11
2676
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
4
2442
by: Gerben van Loon | last post by:
Hello, I'm quite new to Csharp and like to know how I can call a function from an upper class. Following example might explain my problem a bit better: public class MyClass { public void...
17
19262
by: RSH | last post by:
I am really trying to grasp the concept of OOP as it applies to C#. I am looking at trying to set up a simple Employee Class but I am having trouble conceptualizing what this class should look...
4
4177
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
2
2021
by: samadams_2006 | last post by:
I'm trying to get a simple VB.NET "Web Application" (not a regular application) to work with an Access Database. Seems simple enough, but I cannot seem to get it working. If anyone has a simple...
6
4004
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
7
242
by: bvdp | last post by:
I'm finding my quest for a safe eval() quite frustrating :) Any comments on this: Just forget about getting python to do this and, instead, grab my set of values (from a user supplied text file)...
10
2108
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
5788
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
7259
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,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
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,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
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 ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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...

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.