does require belong inside a class?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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

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 ?

Re: does require belong inside a class?

nephish wrote:
Quoted text here. Click to load it

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.

Re: does require belong inside a class?

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


Jerry Stuckle wrote:
Quoted text here. Click to load it

Site Timeline