strazdinjsh Posted December 22, 2010 Share Posted December 22, 2010 would anyone know how to assign the value (value returned by some function in the class) of variable $temp in the class: source looks like: class newRecord{ var $temp = listCat('gallery_menu', 'submenu', 'submenu_id'); ////// this line is not working, does not assign? function listCat($table, $submenu, $submenu_id) { $return = '<select name="'.$submenu_id.'" id="'.$submenu_id.'">'; $result = mysql_query("SELECT * FROM $table"); while($row=mysql_fetch_object($result)) { $return = $return.'<option value="'.$row->id.'">'.$row->$submenu.'</option>'; } $return = $return.'</select>'; return $return; } } if function is used out of the class and used in index.php directly, returns expected value of it (there is no doubts - works ok) Link to comment https://forums.phpfreaks.com/topic/222375-oop-class-assigning-issue/ Share on other sites More sharing options...
trq Posted December 22, 2010 Share Posted December 22, 2010 You need to put it within the classes constructor. class newRecord { private $temp = array(); public function __construct() { $this->temp = $this->listCat('gallery_menu', 'submenu', 'submenu_id'); } // rest of code. } Link to comment https://forums.phpfreaks.com/topic/222375-oop-class-assigning-issue/#findComment-1150269 Share on other sites More sharing options...
strazdinjsh Posted December 22, 2010 Author Share Posted December 22, 2010 thanks a lot, it works. If it is possible, could you tell me in few words the assigning as array - why? what does _construct do in this case? i apologize i am still newbie. Link to comment https://forums.phpfreaks.com/topic/222375-oop-class-assigning-issue/#findComment-1150278 Share on other sites More sharing options...
trq Posted December 22, 2010 Share Posted December 22, 2010 Sorry, $temp shouldn't be setup as an empty array, because it ends up being a string. That's just a blatant mistake on my part. The __construct() is a method which is called automatically when your class is instantiated. Link to comment https://forums.phpfreaks.com/topic/222375-oop-class-assigning-issue/#findComment-1150282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.