Variable Scope in a Class

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all.  Going through some code and trying to clean it up.  This is a class
file that creates a foo object.

Many of the methods of the foo object need db access and make use of a db
object to do so.

function get_foo() {
    global dbObj;

    do some db stuff;

Trying to make this as portable and independant as possible.  Where should I
be creating the dbObj?

- In the calling page? (but the foo obj is then dependant on this being done
first, which seems entirely wrong)

- In the foo constructor? (I can't seem to make this work)

- In each individual foo method? (seems like it would add allot of overhead
by creating and destroying a bunch of new dbObj's)

The question is: I've been searching and can't really find a difinitive
answer.  Looking for advice on best practices and avoiding globals.  Any
advice on how to best access the dbObj from inside the foo methods?



Re: Variable Scope in a Class

GS wrote:
Quoted text here. Click to load it

Two choices - either a singleton or a factory.  You can google for either.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline