hi,
i've got a class that mirrors a database table (variable name -> field name) and i was trying to do something thats been simplified below, but it keeps coming back undefined variable whatever i try.....anyone got any ideas if its possible or not...
cheers
mat
<?php
class foo
{
var $arry, $field1, $field2, $field3;
function foo()
{
$this->arry = array("field1" => "value1",
"field2" => "value2",
"field3" => "value3");
} //constructor
function var_var()
{
$keys = array();
//count cols
$num_cols = count($this->arry);
//get the keys
$keys = array_keys($this->arry);
//try and assign
for ($x = 0; $x < $num_cols; $x++)
{
//get column name
$col_name = $keys[$x];
//column name is same as variable name
//so on first pass $this->$$col_name
//should be the same as
//$this->field1 etc.....
$this->$$col_name = $this->arry[$keys[$x]];
}
}
} //foo
//test it
$test = new foo();
$test->var_var();
echo $test->field1 . "<br>" . $test->field2 . "<BR>" . $test->field3;
?>