Messaging between objects
am 23.01.2008 22:25:31 von Chad Johnson
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...
Re: Messaging between objects
am 24.01.2008 00:42:41 von Jerry Stuckle
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).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Messaging between objects
am 24.01.2008 15:07:21 von zeldorblat
On Jan 23, 6:42 pm, Jerry Stuckle 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.