Entry
how to access an object variable using a variable variable e.g. object->var_a but "_a" should be read from a str. which instead could contain "_b"
Jul 28th, 2000 08:07
Ben Udall, Johan Forsbäck, http://www.php.net/manual/language.variables.variable.php
The best way to do this would probably be to use PHP's variable
variables. The example below illustrates how that would work for
objects.
class obj_def
{
var $var_a;
var $var_b;
}
$object = new obj_def;
$object->var_a = 'Hello ';
$object->var_b = 'world';
$var_name = 'var_a';
print($object->$var_name);
$var_name = 'var_b';
print($object->$var_name);
The output should read 'Hello world'