I have a class for forms. Each form element needs to be instantiated in the head section of the script and then output in the body. The class takes two parameters in the constructor: $name and $notes. $name is the name of the corresponding column in the database (e.g. first_name) and $notes are any custom handling of that element (e.g. required). Here is the code:
$elements = array("first_name" => "required, label","last_name" => "required, label", etc..);
foreach($elements as $key=>$value) {
$$key = new field($key,$value);
}
$$key is a variable variable, and because $key = "first_name" (on the first iteration here) the new variable/object created becomes $first_name
For a better explanation of this:
http://www.php.net/manual/en/language.variables.variable.php