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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.