Jump to content

[SOLVED] Functions in classes returning/accessing variables


Yesideez

Recommended Posts

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.

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;
    }
  }
?>

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

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.