Jump to content

[SOLVED] can on object pass itself as a parameter?


alexweber15

Recommended Posts

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

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...  :-[

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);

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...  :P 

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  :D

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 :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.