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; } } } Link to comment https://forums.phpfreaks.com/topic/77829-solved-creating-a-var-problem/ 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 Link to comment https://forums.phpfreaks.com/topic/77829-solved-creating-a-var-problem/#findComment-393929 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. Link to comment https://forums.phpfreaks.com/topic/77829-solved-creating-a-var-problem/#findComment-393933 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. Link to comment https://forums.phpfreaks.com/topic/77829-solved-creating-a-var-problem/#findComment-393938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.