alexweber15 Posted September 23, 2008 Share Posted September 23, 2008 guess these words are too generic for google basically i wanna know if in a class's methods i can pass the object as a parameter to another function: class Foo{ protected $attr; function __construct($val){ $this->attr = $val; } function sendMe($bar){ if($bar instanceof Bar){ $bar->getFoo(self); } } } class Bar{ function getFoo($foo){ if($foo instanceof Foo){ echo $foo->attr; }else{ echo 'not foo!'; } } } $a = new Foo(4); $b = new Bar(); $a->sendMe($b); this outputs: not foo! i've tried different methods like: $a = new Foo(4); $b = new Bar(); $b->getFoo($a); this outputs: Fatal error: Cannot access protected property Foo::$attr i have no real practical use for this just for learning really is it bad practice for an object to have to pass itself as a parameter to another object? (probably i guess) but even so, when i pass the object explicitly in the second example i still can't access its protected/private properties... (is there any way around this? short of creating public methods to access the properties?) thanks! -Alex Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/ Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Try passing $this. Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648932 Share on other sites More sharing options...
alexweber15 Posted September 23, 2008 Author Share Posted September 23, 2008 Try passing $this. nice tip works the same as in ex2 where i passed the instance manually from the test code... but i still cant access the protected properties... ??? Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648946 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 I mean manually pass $this instead of self inside of the function. Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648949 Share on other sites More sharing options...
alexweber15 Posted September 23, 2008 Author Share Posted September 23, 2008 I mean manually pass $this instead of self inside of the function. yup thats what i did... i don't get the "not foo" error anymore (so its analogous to doing what i did in ex2 except its done inside the first class instead of outside all classes) but i still cant access the private/protected properties... again, i realize this is probably not very good practice because of the really strong coupling but I'm kind of disappointed you apparently can't do this... Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648955 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 One sec, I'll go try it and get back to you. Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648961 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Duh, I wasn't paying attention. Protected properties can only be accessed by child classes and/or the same class that sets it. Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648969 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 This, for example, works: class Foo{ protected $attr; function __construct($val){ $this->attr = $val; } function sendMe(Bar $bar){ $bar->getFoo($this); } } class Bar extends Foo{ function getFoo($foo){ if($foo instanceof Foo){ echo $foo->attr; }else{ echo 'not foo!'; } } function __construct() { } } $a = new Foo(4); $b = new Bar(); $a->sendMe($b); Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-648974 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Any luck? Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-649032 Share on other sites More sharing options...
alexweber15 Posted September 24, 2008 Author Share Posted September 24, 2008 Any luck? hey thanks for your reply and effort! I get it now... makes sense! I thought that somehow having an instance of class Foo as a property would allow class Bar access to its protected methods but now that I think about it its the other way round... as in: if Bar contains an instance of Foo, then Foo should be able to theoretically access Bar's protected variables, because they're in the same scope... but then again i can't think of a way to test this because Foo would have no knowledge that it is one of Bar's properties to begin with and thats a bit too much Foo and Bar for one thread Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-649162 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Lol, glad I could help. Was there any particular reason that you wanted to do this little test? =P Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-649533 Share on other sites More sharing options...
alexweber15 Posted September 24, 2008 Author Share Posted September 24, 2008 Lol, glad I could help. Was there any particular reason that you wanted to do this little test? =P no really it was more of a learning exercise, just testing out some not-so-well documented OO behaviors Link to comment https://forums.phpfreaks.com/topic/125490-solved-can-on-object-pass-itself-as-a-parameter/#findComment-649562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.