|
Posted by Jerry Stuckle on 08/17/06 12:28
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.
jstucklex@attglobal.net
==================
[Back to original message]
|