Connecting Tech Pros Worldwide Forums | Help | Site Map

Class / file architecture in PHP 4

Paul
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi there,
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
B. Create a new dbaccess object at the top of the file containing all
the classes and make it global (security implications?)
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).

I'm trying to balance security, elegance and efficiency...

Cheers,
Paul.

Michael Fesser
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Class / file architecture in PHP 4


.oO(Paul)
[color=blue]
>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[/color]

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

This is nearly how I would do it, see below.
[color=blue]
>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).[/color]

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.

Micha
Jean-Marc Molina
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Class / file architecture in PHP 4


Michael Fesser a écrit/wrote :[color=blue]
> 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.[/color]

Definitely the best pattern. To learn more about singleton and find some
good implementation examples :
- http://www.php.net/manual/en/language.oop5.static.php
- http://www.phppatterns.com/index.php...cleview/6/1/1/

--
Jean-Marc.

Chung Leong
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Class / file architecture in PHP 4


"Michael Fesser" <netizen@gmx.net> wrote in message
news:vr3s11l9u45brv2jqd7bh10aoqhf81oofd@4ax.com...[color=blue]
> .oO(Paul)
>[color=green]
> >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[/color]
>
> Nope. This would probably require a new DB connection in each class.
>[color=green]
> >B. Create a new dbaccess object at the top of the file containing all
> >the classes and make it global (security implications?)[/color]
>
> This is nearly how I would do it, see below.
>[color=green]
> >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).[/color]
>
> 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.[/color]

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().


Jean-Marc Molina
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Class / file architecture in PHP 4


Chung Leong a écrit/wrote :[color=blue]
> 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.[/color]

Good one :). You should definitely re-read some references about the
subject.

--
Jean-Marc.

Closed Thread