2009/6/30 Eddie Drapkin <oorza...@gmail.com> > It should be passed into the constructor as a parameter. If you're > using OOP properly, there's no reason to use $GLOBALS, ever. Any > variable in the $GLOBALS array exists twice in memory, so just keep > that in mind, if you plan to use it. > > On Tue, Jun 30, 2009 at 8:43 AM, Peter Ford<p...@justcroft.com> wrote: > > Luke wrote: > >> Hello again guys, > >> > >> I was wondering the best way to tackle the following problem: > >> > >> I've got a class, containing a property which is another object. So from > >> outside I should be able to do > >> $firstobject->propertycontainingobject->methodinsidethatobject(); > >> > >> The $firstobject variable is in the global namespace (having been made > with > >> $firstobject = new FirstObject;), and I'm having a problem that I'm sure > >> many people have when accessing it inside another class, so: > >> > >> class otherObject > >> { > >> static function messwithotherthings () > >> { > >> $firstobject->propertycontainingobject->methodinsidethatobject(); > >> } > >> } > >> > >> But $firstobject is blank, which makes sense because in there it is > pointing > >> to the local variable within the method. > >> > >> To solve this, I could add 'global $firstobject' inside every method, > but > >> this is very redundant and boring. I've tried a couple of things like > >> adding: > >> > >> private $firstobject = $GLOBALS['firstobject']; > >> > >> But apparently that's bad syntax. I was just wondering the best way to > get > >> around this? > >> > >> Thanks a lot for your help, > >> > > > > > > Set the value of $firstobject in the constructor of otherObject (that's > what > > constructors are for!): > > > > eg. > > > > class otherObject > > { > > private $firstobject; > > > > function __construct() > > { > > $this->firstobject = $GLOBALS['firstobject']; > > } > > > > static function messwithotherthings () > > { > > > $this->firstobject->propertycontainingobject->methodinsidethatobject(); > > } > > } > > > > > > -- > > Peter Ford phone: 01580 893333 > > Developer fax: 01580 893399 > > Justcroft International Ltd., Staplehurst, Kent > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Thanks for the replies :)
Surely if I pass it as a parameter to the __construct then I would have to make an instance of the otherObject, notice that messwithotherthings is static? Also, if I'm not using OOP properly, Eddie, how would I use it properly to prevent this situation? Thanks, -- Luke Slater :O)