Tom Rogers <[EMAIL PROTECTED]> wrote... : > > Amazing what you learn on this list :) > This works: > > class Example > { > var $array1 = array(); > var $array2 = array(); > var $array3 = array(); > > function add2array($array_name,$val){ > $this->{"$array_name"}[] = $val; > } > > } > $t = new Example(); > $t->add2array('array1',25); > $t->add2array('array2',26); > $t->add2array('array3',"Hello"); > echo '<pre>'; > print_r($t); > echo '</pre>'; >
Yet, this is not a such elegant way doing it. It can be helpful in a lot of cases, but most often, element key is enough. Try rethinking your logic: class Example { var $array = array(); function add2array($element_name, $val){ $this->$array[$element_name] = $val; } } $t = new Example(); $t->add2array('array1',25); $t->add2array('array2',26); $t->add2array('array3',"Hello"); echo '<pre>'; print_r($t); echo '</pre>'; Cleaner and more scalable, no? -- Maxim Maletsky [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php