PHP Associative arrays with objects as values?
Posted: Thu Nov 13, 2008 9:48 am
I'm having some trouble with a function in one of my frameworks. I changed the backend to use a properties-based approach with registerable fields instead of extending the class and adding the fields there. However, the function to actually register the field is causing a problem...
When I pull up the page, I see the following:
Other code that might be pertinent:
(Also, the Course constructor calls $this->RegisterField() on the autoindex; it is not explicitly called after construction. $fields is defined earlier as "$fields = new array();")
Thanks!
Code: Select all
class Course {
// snip...
public function RegisterField($fieldName, $fieldDesc){
echo "Registering field '$fieldName' as '$fieldDesc'... ";
$this->$fields[$fieldName] = new DataField($fieldDesc);
echo "Registered.\n";
}
// snip...
but it never gets to "Registered", and doesn't register any of the fields after that one. I'm not sure why this doesn't work, and from what I've read I can use objects as values in an associative array. Can someone explain what's wrong and/or how to fix it (if it's easily reconciled)?Registering field 'autoindex' as 'int( 11 ) NOT NULL AUTO_INCREMENT'...
Other code that might be pertinent:
Code: Select all
class DataField {
// snip...
function DataField($field){
$this->fieldDesc = $field;
$this->fieldValue = "";
}
// snip...
}
Thanks!