Yesideez Posted May 17, 2007 Share Posted May 17, 2007 Hi, quite new to classes and trying to get my head round them by playing with some silly examples In my config include file is this: class clsHTML { var $arrGenders=array('X','Male','Female','Both'); function makeoptsGender() { $optGender=''; for ($i=1;$i<count($this->$arrGenders);$i++) { $optGender.='<option value="'.$i.'">'.$this->$arrGenders[$i].'</option>'; } return $optGender; } } In one of the main scripts I've got code like this: $clsHTML=new clsHTML(); $optGender=$clsHTML->makeoptsGender(); No matter what I try and do I can't get anything returned by my function. I've even tried using echo inside the makeoptsGender() function to display text and nothing. What I should be getting returned is something like this: <option value="1">Male</option><option value="2">Female</option><option value="3">Both</option> If anyone is able to help point out what I've done wrong I'd be really grateful, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/51808-solved-functions-in-classes-returningaccessing-variables/ Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 change $this->$arrGenders to $this->arrGenders <?php class clsHTML { var $arrGenders=array('X','Male','Female','Both'); function makeoptsGender() { $optGender=''; for ($i=1;$i<count($this->arrGenders);$i++) { $optGender.='<option value="'.$i.'">'.$this->arrGenders[$i].'</option>'; } return $optGender; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51808-solved-functions-in-classes-returningaccessing-variables/#findComment-255251 Share on other sites More sharing options...
vbnullchar Posted May 17, 2007 Share Posted May 17, 2007 <?php class clsHTML { var $arrGenders=array('X','Male','Female','Both'); function makeoptsGender() { $optGender="<select>"; foreach ($this->arrGenders as $value) { $optGender.="<option value=$value>$value</value>"; } $optGender.="</select>"; return $optGender; } } $clsHTML=new clsHTML(); $optGender=$clsHTML->makeoptsGender(); echo $optGender; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51808-solved-functions-in-classes-returningaccessing-variables/#findComment-255253 Share on other sites More sharing options...
Yesideez Posted May 17, 2007 Author Share Posted May 17, 2007 I can't believe I made such a daft mistake! Many thanks for pointing that out - all working fine now Quote Link to comment https://forums.phpfreaks.com/topic/51808-solved-functions-in-classes-returningaccessing-variables/#findComment-255254 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.