dynamically set variable?
Posted: Fri Sep 17, 2004 11:19 am
This is a php 4 question, I know php 5 is introducing reflection, but my web host isn't supporting 5 yet, so I'd like to stick to 4 for now.
Is there a way to set a variable if I have the variable name as a string? I'd like to make a utility for my constructor to use that would take an associative array like this:
array("id" => 1, "name" => "Joe");
I'd like to have the constructor loop through the array, and if there is a member variable with the same name as the key, set the value. (In the array above, my object would get $this->id and $this->name set).
Is there any way to do this?
Thanks.
Is there a way to set a variable if I have the variable name as a string? I'd like to make a utility for my constructor to use that would take an associative array like this:
array("id" => 1, "name" => "Joe");
I'd like to have the constructor loop through the array, and if there is a member variable with the same name as the key, set the value. (In the array above, my object would get $this->id and $this->name set).
Is there any way to do this?
Thanks.