faqts : Computers : Programming : Languages : PHP : kms : Classes

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 12 people (58%) answered Yes
Recently 4 of 8 people (50%) answered Yes

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'