On Tue, Jun 30, 2009 at 10:44 AM, Luke<luke@xxxxxxxxxxxxxx> wrote: > > > 2009/6/30 Eddie Drapkin <oorza2k5@xxxxxxxxx> >> >> 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<pete@xxxxxxxxxxxxx> 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) > > Well all you'd really need to do is something like this (for an instance class): class ExampleClassYay { private $instance; public function __construct(someOtherClass $instance) { $this->instance = $instance; } public function foo() { var_dumP($this->instance); } } So, all you'd have to do is something like: $foo = new someOtherClass(); $example = new ExampleClassYay($foo); $example->foo(); Whereas, for a static class, you can use a static property accessed via self::$property instead of $this->property, like so: Well all you'd really need to do is something like this: class StaticExampleClassYay { private static $instance; static public function setInstance(someOtherClass $instance) { self::$instance = $instance; } static public function foo() { var_dump(self::$instance); } } In which case, it'd be something like $foo = new someOtherClass(); StaticExampleClassYay::setInstance($foo); StaticExampleClassYay::foo(); What I meant, and I probably sounded a bit rougher than I absolutely had to, was that it's pretty universally lazy and/or bad design to use global variables inside a class, because classes have member properties. From a purely philosophical standpoint, classes are supposed to be entirely self-contained and any data that a method needs to process is supposed to be passed as a parameter, or exist inside the class as a property. It helps to think of classes like cars on a highway; if you want it to go faster, you give it some gas (pass a method a parameter), but if you want to turn on some music while driving, the CDs should already exist in the car (accessing a member property) even though they might have been added earlier (via a different method with other parameters). That's probably not the greatest metaphor, but I hope you see what I'm saying. --Eddie PS. sorry for sounding rough :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php