xyn Posted November 18, 2007 Share Posted November 18, 2007 hey guys. Basically i've got a class. but im having a problem setting class vars using an array... i get a T_VAR error when using $this->$name. my current code is... Page 1.php $template = new Class; $array =array( 'name' => 'value', 'name_b' => 'value_2' ); $template->set_vars($array); and my class is : class Class { function assign_vars($array){ foreach($array as $name => $value){ return $this->$name = $value; } } } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 18, 2007 Share Posted November 18, 2007 It should be $this->name not $this->$name Quote Link to comment Share on other sites More sharing options...
xyn Posted November 18, 2007 Author Share Posted November 18, 2007 yer i know.. but the $name comes from the array foreach($array as $name => $value){ anyway, ive decuded to use this... function assign_varname($str){ return $this->varname = $str; which varname will be changed to what the name actually is... I was initially hopeing to be able to use an array. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 18, 2007 Share Posted November 18, 2007 You might want to use return $this->$name = $value; instead then. Quote Link to comment 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.