addressalign-toparrow-leftarrow-leftarrow-right-10x10arrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblightning-boltlinklocation-pinlockm-swarmSearchmailmediummessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstar-shapestartickettrashtriangle-downtriangle-uptwitteruserwarningyahooyoutube

Re: [php-49] Variable Passing between Classes

From: Eric H.
Sent on: Wednesday, May 9, 2012, 11:25 AM
Without knowing it, I was already using the basic premise behind "Dependency Injection", I had not taken it the extra step of separating it out into it's own object. But some how that still does not feel like the "best" case. 

Who all has used the "Dependency Injection" design pattern?

-- Eric

Sr. Software Engineer
ZCE ~ Zend Certified Engineer


On Tue, May 8, 2012 at 9:34 PM, Eric Harris <[address removed]> wrote:
Thanks guys. I totally agree $GLOBALS or global is not a great way to go. While those work and do the job, they don't feel like they are the best.

I agree, Jeremy, taking a better look at how that data is used and perhaps reworking that would make since. As I think about what most of that data is used for, it is mainly used for building href links and building queries. So I know I could refactor all that out into a self contained link building method. I will have to look into dependency injection.

I had looked over the "Observer Pattern", but I was failing to see how that could be helpful for my situation. But I think that I am going to have to re-exam that pattern.

My other thought was to make my data class, which does all db queries and is most dependant on the data, responsible for all those variables. Since nearly every other class utilizes the data class, it would then make it simple to allow that class responsible for all other data. Don't know if that is still skirting around the "best" practice, but seems like a logic method for dealing data.

How does that approach sound? 

-- Eric

Sr. Software Engineer
ZCE ~ Zend Certified Engineer


On Tue, May 8, 2012 at 8:41 PM, Ariz Jacinto <[address removed]> wrote:

Hi Eric,

It's a good thing that you are moving away from $GLOBALS (and please
don't be tempted to use 'global' as well). I believe using a singleton
should suffice but it seems like you are still looking for a
subjective "best" solution. We can only help you better if you list
all the criteria or use-cases though.

If i'm going to guess the requirement based on your statement "...when
you get into needing data across multiple classes, especially when
data is not available until later in the execution cycle, this becomes
complex and a bit hairy when trying to pass that data around to
various classes, that might need it..." then i think you might find
the Observer pattern[1][2] useful since it allows you to notify all
the class observers to act on a data change. I hope I read your mind
correctly.

Ref:
[1] http://www.phppatterns.com/docs/design/observer_pattern
[2] http://www.ibm.com/developerworks/library/os-php-designptrns/



--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
http://www.meetup.com/php-49/
This message was sent by Ariz Jacinto ([address removed]) from The Seattle PHP Meetup Group.
To learn more about Ariz Jacinto, visit his/her member profile: http://www.meetup.com/php-49/members/3108011/
Set my mailing list to email me

As they are sent
http://www.meetup.com/php-49/list_prefs/?pref=1

In one daily email
http://www.meetup.com/php-49/list_prefs/?pref=2

Don't send me mailing list messages
http://www.meetup.com/php-49/list_prefs/?pref=0
Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]






--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Eric Harris ([address removed]) from The Seattle PHP Meetup Group.

To learn more about Eric Harris, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

People in this
group are also in: