NeoMarine Posted November 21, 2008 Share Posted November 21, 2008 I am trying to get something like this to work: function blablabla($var1, $var2, $var3, $pathToTempate){ $DisplayModule = new DisplayModule(); foreach (get_defined_vars() as $key => $value){ $DisplayModule -> $key = $value; } return $DisplayModule -> render($pathToTempate); } echo blablabla("hey","there","friend","files/template1.php"); The code for the DisplayModule class is: class DisplayModule { public function render($displayPath) { ob_start(); $this->_include($displayPath); return ob_get_clean(); } public function __get($key) { return (isset($this -> $key) ? $this -> $key : null); } protected function _include() { include func_get_arg(0); } } Link to comment https://forums.phpfreaks.com/topic/133623-passing-all-variables-passed-in-function-to-another-classs-function/ Share on other sites More sharing options...
mtoynbee Posted November 21, 2008 Share Posted November 21, 2008 Maybe you should explain further what you are trying to do. Link to comment https://forums.phpfreaks.com/topic/133623-passing-all-variables-passed-in-function-to-another-classs-function/#findComment-695246 Share on other sites More sharing options...
samshel Posted November 21, 2008 Share Posted November 21, 2008 the logic you have posted should work, however it is not the best way to do it. You should have set methods for all variables in DisplayModule class and call them. Have you tried it already, what is the problem? Link to comment https://forums.phpfreaks.com/topic/133623-passing-all-variables-passed-in-function-to-another-classs-function/#findComment-695294 Share on other sites More sharing options...
NeoMarine Posted November 21, 2008 Author Share Posted November 21, 2008 Purely for the sake of not having to write [echo $this->var1] inside the $displayPath file to get the output. I would much rather enter [echo $var1]. Previously I was using global and a single function, but then I found online that a class (DisplayModule) would be better suited, the only problem is I am so used to coding without classes (in fact I am not sure how to use them entirely). Could you give me an example of "set methods for all the variables in DisplayModule", samshel? I'm trying to get DisplayModule to work with any kind of variable, if I had to set them individually that would defeat the purpose I think. Maybe I'm being too silly for semantic reasons, and should just bite the bullet and use $this->var1 ... anyway, the problem with my above script is that foreach section of code. $key needs to be 'var1' and $value should be 'hey', so I'm trying to have it write $DisplayModule -> var1 = 'hey', but obviously $key (var1) is being returned as a string and the script doesn't like that... perhaps I have to use something like var[$key], or classvar[$key], but I dont think those conversions exist. Thanks for your help, if this is not possible or it looks like its only going to slow my script down, I'll just bite the bullet and start getting used to calling referenced variables. Link to comment https://forums.phpfreaks.com/topic/133623-passing-all-variables-passed-in-function-to-another-classs-function/#findComment-695561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.