I am using the following code to load my object classes;
function __autoload($class_name) {
require_once ( 'classes/'.$class_name.'.class.php');
}
If would like it so that if it can't find the file, it goes to the Database, finds the table of the data item i'm trying to access. And creates a standard object on the stop which contains the variables which represent the columns in the DB. Is this possible? I know how to check if the file exists with PHP, but I'm not quite sure how I would return the new standard object once initiated with the correct variable names.
Many thanks.
Create a function that returns all column names for a table as an array (no database specs given, maybe MySQL?). Then initialize an object with empty values (NULL
) for each of those:
$names = get_table_column_names($table);
$values = array_fill(0, count($names), NULL);
$obj = (object) array_combine($name, $values);
$obj
is a standard PHP object containing those columns.
However, this won't work with autoload, as you need to create the class definition on the fly to reflect the type. I would say, you need to inject it via eval
/include
with public properties initialized to NULL by default.