|
Posted by ZeldorBlat on 01/24/08 14:07
On Jan 23, 6:42 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Chad Johnson wrote:
> > Suppose we're using an object-oriented framework for our web site.
> > We're obviously going to have a few common objects used throughout the
> > framework, such as User and DB (or just a DB connection link). How
> > would you go about making these objects available inside of the member
> > functions of your classes? Would you encapsulate them some common
> > object, like "Settings" or "Context"? Would you make these objects
> > available inside member functions via "global $VARIABLE_NAME;"? Pass
> > them individually or the Settings/Context object as a whole as a
> > constructor parameter for every object in your framework?
>
> > Surely someone else has faced this problem before...
>
> Chad,
>
> It all depends on what I need.
>
> First of all, I don't encapsulate them in a pseudo-object. You
> generally don't need all objects in all cases, so putting them all in a
> single object just adds overhead.
>
> I also don't use globals. It makes maintenance and troubleshooting harder.
>
> Generally I will pass the objects in the constructor, a member function
> or a setter function. For objects which are more global (i.e. a
> database object which may be used by several classes) I might create a
> singleton (google for it).
>
> --
I think singletons are the way to go here. I use them for the
database and session (and the session object has the user object) as
well as a few other things. For instance, whenever I need the
database I just do something like this:
$db = DB::get();
That can be called from anywhere so there's no need to pass it around
as a parameter or use a global.
Navigation:
[Reply to this message]
|