You are here: Re: does require belong inside a class? « PHP Programming Language « IT news, forums, messages
Re: does require belong inside a class?

Posted by nephish on 08/17/06 14:21

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.
> jstucklex@attglobal.net
> ==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация