Jump to content

Passing all variables passed in function to another class's function


NeoMarine

Recommended Posts

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

}

}

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.

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.