Re: Messaging between objects
- From: Chad Johnson <chad.d.johnson.work@xxxxxxxxx>
- Date: Sat, 16 Feb 2008 18:17:14 -0800 (PST)
On Jan 24, 8:07 am, ZeldorBlat <zeldorb...@xxxxxxxxx> wrote:
On Jan 23, 6:42 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> 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.
This is what I went with: http://www.patternsforphp.com/wiki/Registry
.
- Prev by Date: Re: dns_get_record()
- Next by Date: Re: Code
- Previous by thread: dns_get_record()
- Next by thread: This week is full of job interviews from most preferred companies - visit www.northvacancies.com
- Index(es):
Relevant Pages
|