razorsese Posted January 18, 2013 Share Posted January 18, 2013 I have the following code: My question is how i can return both the y and the z in magic function.( works with y alone) class X{ private y; private z; public function __construct($y,$z) { $this->y = $y; $this->z = $z; } public function magic() { return $this->{ $this->y }(); //need to return both Y & Z } } Quote Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/ Share on other sites More sharing options...
iarp Posted January 18, 2013 Share Posted January 18, 2013 return array($this->y, $this->z); list($myOwnVarForY, $myOwnVarForZ) = $X->magic(); Quote Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/#findComment-1406793 Share on other sites More sharing options...
requinix Posted January 18, 2013 Share Posted January 18, 2013 (edited) By the way, return $this->{ $this->y }(); is quite wrong. Even if it's syntactically valid (might be) it won't do what you want it to do. Edited January 18, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/#findComment-1406796 Share on other sites More sharing options...
razorsese Posted January 18, 2013 Author Share Posted January 18, 2013 Yep. return $this->{ $this->y }(); Actually return the function with name Y. Quote Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/#findComment-1406834 Share on other sites More sharing options...
requinix Posted January 18, 2013 Share Posted January 18, 2013 Just checked, it is valid. Actually return the function with name Y. Call the function named whatever the value of $this->y is. call_user_func(array($this, $this->y)); Quote Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/#findComment-1406847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.