View Single Post

  #4 (permalink)  
Old 02-26-2005
Chung Leong
 
Posts: n/a
Default Re: Class / file architecture in PHP 4

"Michael Fesser" <netizen@gmx.net> wrote in message
news:vr3s11l9u45brv2jqd7bh10aoqhf81oofd@4ax.com...
> .oO(Paul)
>
> >Quick question please. I have a file containing a number of re-usable
> >classes. Each require database access. Seperately, I have a database
> >class called dbaccess. Is it best to:
> >
> >A. Create a new dbaccess object in each class

>
> Nope. This would probably require a new DB connection in each class.
>
> >B. Create a new dbaccess object at the top of the file containing all
> >the classes and make it global (security implications?)

>
> This is nearly how I would do it, see below.
>
> >C. Create the dbaccess object in the calling PHP script, and pass a
> >reference to the constructor method of each class.
> > E.g. $widget = new class1(&$dbAccessor).

>
> Possible, but not necessary. I prefer
>
> D. The DB class implemented as a singleton. This way there's always only
> one instance of it, which is globally available for all other classes
> that may need DB access.


A singleton is just a dressed up global variable. It carries the same
limitation as a global variable. Other than the cachet of being a design
pattern ("look ma, I've read the book!"), it provides little.

In a good design, objects that represent data are independent of objects
representing the data source. A good litmus test is whether the objects can
retrieve data from one database and save to another. If not, then you might
as well stick with using mysql_query().


Reply With Quote