473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best practice for managing object oriented database connections

18
I've often struggled with database calls--not from a coding standpoint, but from the standpoint of how to make them more self-maintaining. My dream would be this:
  • An include file which contains the database connection code
  • A class within this include file that contains a class
  • A constructor which connects to the database
  • a destructor which closes the connection

This would be perfect if PHP called the destructor once your class object leaves scope. Then all you'd have to do it place the include file at the top of a page, instantiate an instance of the class where needed, and the server would disconnect from the database at the first appropriate moment. This would be beautifully clean since a parameter sent to the constructor could affect the database user one connects with--a great way to control read/write permissions. I haven't updated to PHP 5 yet, but my understanding is that one must still make an explicit call to the destructor. Please correct me if I am wrong!

That said, how do you personally manage database connections (specifically disconnecting)? Do you use classes at all? A particularly annoying problem for me is when I initialize a connection, close it, and then realize a parent block still wanted to use the database. Oops...
Apr 21 '08 #1
1 3629
dlite922
1,584 Expert 1GB
I've often struggled with database calls--not from a coding standpoint, but from the standpoint of how to make them more self-maintaining. My dream would be this:
  • An include file which contains the database connection code
  • A class within this include file that contains a class
  • A constructor which connects to the database
  • a destructor which closes the connection
This would be perfect if PHP called the destructor once your class object leaves scope. Then all you'd have to do it place the include file at the top of a page, instantiate an instance of the class where needed, and the server would disconnect from the database at the first appropriate moment. This would be beautifully clean since a parameter sent to the constructor could affect the database user one connects with--a great way to control read/write permissions. I haven't updated to PHP 5 yet, but my understanding is that one must still make an explicit call to the destructor. Please correct me if I am wrong!

That said, how do you personally manage database connections (specifically disconnecting)? Do you use classes at all? A particularly annoying problem for me is when I initialize a connection, close it, and then realize a parent block still wanted to use the database. Oops...
Don't know why you see this as a problem. Create the class and with every instantiation, the database connects.

What I do if I want to use the same database link/connection for multiple objects i can just pass the link to the constructor.

If the link is valid, use it for that new instance, if not, create a new one.

The connection will close once PHP is done. You can make multiple connections.

If you make alot of them and want to close each one, just have a method in your class that closes it!

$myDB->close();

You can just call it after you're done using that instance (ie when you want to sign on with a different username)

Why would you want to connect with different usernames in one PHP call? I just don't see a reason for it.

When an admin type user connects you'll use the root login for example and if a regular user connects you can use a less privilaged mysql user then.

MAX 2 users in mysql and you probably don't have to use both of them in one server call.
Apr 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its...
20
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are...
2
by: philip.mckee | last post by:
Hi Looking for some very general advice. I have a classic asp content management application which I am porting to ASP.NET. The database is SQL server, and it is hoped that we can leverage this...
1
by: DelphiLover | last post by:
Hi. I'm reading and reading, testing and testing, trying to figure out how to do things, how to do things according to best practises and how to do things in the best object oriented way. ...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
1
Plater
by: Plater | last post by:
I don't think I brought this up before, so I'm going to ask it. My recent style of using a database in my applications (web-based and windows-based) has been to create a static class called...
3
by: Venkat | last post by:
Hi, We have a windows application developed in c# and SQL Server 2005. Our application need to execute more than one command (ExecuteReader and ExecuteScalar) at a single time. Till now we have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.