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. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
.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 |
|
|||
|
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. |
|
|||
|
"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(). |
|
|||
|
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. |