hi,
i have a class file that have all values from a database fields.
for example:


class mospedidos extends mosDBTable {
id = null;
date =null;
}

class mosunder extends mosDBTable {
id = null;
name =null;
}

each time i creat a new field in the database i need add the same field in the
class file, what i want is a way using php to add that new field to the class file.
i just need a way to open the file and the class name that i give and add the value
and save the file.
how can i do this?
thanks for your help
:)

Dani AI

Generated

For : two practical ways to stop editing class files by hand every time the schema changes — a small generator that emits the PHP class from the live schema, or switch the class to hold dynamic data (so the code reads columns at runtime). already suggested reading columns from the database and mentioned an ORM; both are valid. Below are concrete steps, a minimal generator example, and a compact dynamic alternative.

A safe generator workflow

  • Query the table schema (information_schema or SHOW COLUMNS).
  • Sanitize column names so they are valid PHP identifiers.
  • Build the class source in memory, write it to a temp file, then atomically rename to the real file.
  • Keep a backup and run the generator from dev or as an admin action (not automatically on every request).
    Example minimal generator (trimmed for clarity):
$pdo = new PDO('mysql:host=localhost;dbname=mydb','user','pass');
$cols = $pdo->query("SHOW COLUMNS FROM `{$table}`")->fetchAll(PDO::FETCH_COLUMN, 0);
$props = "";
foreach ($cols as $c) {
  if (!preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/',$c)) continue;
  $props .= "    public \${$c} = null;\n";
}
$php = "<?php\nclass {$className} {\n{$props}}\n";
file_put_contents($file . '.tmp', $php, LOCK_EX);
rename($file . '.tmp', $file);

A more maintainable approach
Make the model dynamic: store row data in an array and implement __get/__set (or use PDO::FETCH_OBJ, ArrayAccess, or an ORM). That removes the need to regenerate files and is friendlier to frequent schema changes. Example pattern: a small wrapper that maps any column to $obj->column.

Cautions and tips

  • Always sanitize column names and avoid reserved words.
  • Use atomic writes and backups.
  • If your server uses an opcode cache (opcache/APC), invalidate it or restart PHP after writing new files.
  • Consider migrations or an ORM for long-term stability.

If the goal is speed and minimal change now, use the generator. If the schema truly changes often, invest in a dynamic model or an ORM for better maintainability.

Recommended Answers

All 9 Replies

Member Avatar for Member #120589

You could have an include file I suppose. Is there any reason why your DB needs redesigning so often?

Would an ORM be any use, like Doctrine?

hi,
the app is growing and everyday i must add new fields.
i have seen your sugestion thanks for that but i looking for a more simple way
to do this, i just want a php script to open the class file and open the class that i request and add the new value that i say.
thanks :)

hi,
the app is growing and everyday i must add new fields.

App growing or bad design? App growing should at worst add new table not alter a table.
I think it is time to check your design!

commented: My thoughts exactly +14

evstevemd i agree with you.

hi,
i don´t think it´s bad design because
each week a new person ask about a new function or fields that don´t already exist :)

If the class is a one off copy of the table, why not let the class load it's columns and default values from the table. See the mysql manual for more information.

From the PHP side, I suggest you ditch your class properties, and switch to the __get() magic method.

Even in an agile development environment, they will not have database changes each week. Not necessarily bad design, but definitely bad management (IMHO).

hi,
i don´t think it´s bad design because
each week a new person ask about a new function or fields that don´t already exist :)

What kind of application is that? Definitely it is bad design. You cannot take toyota Mark II blue print and add new feature and say there was no design shortage. I don't want to enter into arguments but it is either bad design or it is entirely new app I have never heard of!

and if you mean a library where one can add nore functionality then check with inheritance!

srdva59 you shouldn't feel bad about the post but all means the fact. You can be more elaborate about your design may be someone might have a better idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.