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 } } 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(); 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 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. 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. 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)); Link to comment https://forums.phpfreaks.com/topic/273335-oop-return-this-question/#findComment-1406847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.