473,400 Members | 2,145 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,400 software developers and data experts.

does require belong inside a class?

hey there,
ok, i am starting with building my Customer custom php class.
now, one funciton that i want to write will populate the class
variables with values from the database. i have a function that makes
my connection for me. This is so i dont have the connection code in the
web_root folder .
so in most of my php pages i have something like
require('/var/common_php/db_connect.php');

then have $conn = make_db_connection();

ok, so now i have this class, the class is going to be the only class
of the php file that will be saved as the same name. Class Customer in
Customer.php. So, where do i put my connection function ?
inside the class? or just in the same file? or in the php page that
calls the class ?
what would be best ?
thanks

Aug 17 '06 #1
2 1456
nephish wrote:
hey there,
ok, i am starting with building my Customer custom php class.
now, one funciton that i want to write will populate the class
variables with values from the database. i have a function that makes
my connection for me. This is so i dont have the connection code in the
web_root folder .
so in most of my php pages i have something like
require('/var/common_php/db_connect.php');

then have $conn = make_db_connection();

ok, so now i have this class, the class is going to be the only class
of the php file that will be saved as the same name. Class Customer in
Customer.php. So, where do i put my connection function ?
inside the class? or just in the same file? or in the php page that
calls the class ?
what would be best ?
thanks
That's a matter of programming style. You could call it from a class
member, i.e. the constructor. But this is likely to be inefficient -
multiple objects would each make their own connections, instead of
reusing an existing connection. You can get around this to some extent
with static variables, but let's stay off that subject for now.

The way I prefer is to have the program create a Database object and
pass it as a parameter to the constructor or fetch() function. That way
all objects can use the same connection.

The one thing you don't want to do is include your make_db_connection()
function in the class file, but outside of the class. Every time you
include the file you will create a new connection, whether you're going
to use it or not. And if you have several classes like this you will
get several connections.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 17 '06 #2
Hey thanks for that,
thats what i will do. I didn't give much thought to the multiple
connection problem before. i had some code early on that would make
dozens of connections then eventually fail out. Took a while for me to
figgure out that there are a finite number of conns you can make to
MySQL before it shuts you down.
I like your idea,
thanks much

shawn

Jerry Stuckle wrote:
nephish wrote:
hey there,
ok, i am starting with building my Customer custom php class.
now, one funciton that i want to write will populate the class
variables with values from the database. i have a function that makes
my connection for me. This is so i dont have the connection code in the
web_root folder .
so in most of my php pages i have something like
require('/var/common_php/db_connect.php');

then have $conn = make_db_connection();

ok, so now i have this class, the class is going to be the only class
of the php file that will be saved as the same name. Class Customer in
Customer.php. So, where do i put my connection function ?
inside the class? or just in the same file? or in the php page that
calls the class ?
what would be best ?
thanks

That's a matter of programming style. You could call it from a class
member, i.e. the constructor. But this is likely to be inefficient -
multiple objects would each make their own connections, instead of
reusing an existing connection. You can get around this to some extent
with static variables, but let's stay off that subject for now.

The way I prefer is to have the program create a Database object and
pass it as a parameter to the constructor or fetch() function. That way
all objects can use the same connection.

The one thing you don't want to do is include your make_db_connection()
function in the class file, but outside of the class. Every time you
include the file you will create a new connection, whether you're going
to use it or not. And if you have several classes like this you will
get several connections.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 17 '06 #3

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

Similar topics

2
by: .pd. | last post by:
If I have a control that adds itself to its container, does that break encapsulation? Is it a bad thing? Here's what I mean: public class fred { public barny b; public fred() {
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
3
by: nicver | last post by:
I am fixing a client's Web site and for some reason an ASP class does not want to use the variables it retrieves when it initialiases. This is an excerpt of the class and it is enough to show...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
7
by: Manfred Kooistra | last post by:
I have these pieces of example code that I am trying to figure out: (a) $a = $b->c(); (b) $d->e(); What does "->" do? Where is there a reference/explanation in the PHP manual (or anywhere...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.