Class / file architecture in PHP 4

This is a discussion on Class / file architecture in PHP 4 within the PHP Language forums, part of the PHP Programming Forums category; Hi there, Quick question please. I have a file containing a number of re-usable classes. Each require database access. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-24-2005
Paul
 
Posts: n/a
Default Class / file architecture in PHP 4

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.
Reply With Quote
  #2 (permalink)  
Old 02-24-2005
Michael Fesser
 
Posts: n/a
Default Re: Class / file architecture in PHP 4

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

Micha
Reply With Quote
  #3 (permalink)  
Old 02-25-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Class / file architecture in PHP 4

Michael Fesser a écrit/wrote :
> 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.


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.

Reply With Quote
  #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
  #5 (permalink)  
Old 02-26-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Class / file architecture in PHP 4

Chung Leong a écrit/wrote :
> 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.


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

--
Jean-Marc.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0