I am stuck with a very old large website which I need to do some modifications. I can't use or integrate a new MVC framework due to current site is outdated with some custom coding done long time ago.

I am writing custom reflection class for the user table in the database, but I find it easy to work on the model creating when the parameter passed or primary key and populated the member variables , then use them in the code. for example

$user= new User($user_id);

and then if I need the age I can use

if($user->age > 18)

To do this you might say it's easy just create a constructor method and do a select and assign them to the member variables .. however tricky part is

I want to

  1. Even the database table design is changed I want them to be reflected in the coding without altering the code, ie. if we add a new field to the database table I want it to be called using $user->new_field; without touching the code. (I know Yii can do this but I can't use Yii as I have to write all functionality of the website if I have to move to a framework)

  2. if possible assign them to the member variables with the data type i.e Strings, Int etc reading them from the database

How to build a model base on above (actually constructor method)?

2 Answers 2



Focus on writing decent classes for data access. The time you will spend putting a few select, insert, update, and delete queries in class definitions will likely pale in comparison to the amount of time you will spend automating it, not to mention the bugs you will likely introduce.

Using schema to generate classes at runtime is possible, but why? Schema and code are locked together like hand and glove -- one cannot change without the other.

  1. Something along the lines of...

    foreach ( $db_rec as $db_field=>$db_val) {
        $this->$db_field = $db_val;

    classes can create their own properties on the fly.

  2. PHP isn't strictly typed. (If memory serves, one can only typecast as arrays and objects anyway) But if you really to determine types from database, I suppose you could pull it out of DESC myTable;

    This doesn't smell like a good idea though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.