I have a class that enforces database-friendly formatting for some of its properties in the
This guarantees that if a new object is created and saved, it will be saved correctly. But saving new objects happens rarely. Retrieving them happens thousands of times more frequently, and these same rules are applied to the db-extracted data when it goes through
__construct(), a waste of resources.
Perhaps a bit of regex is not something I should worry about, but it bothers me. Am I doing this right?
If all of this stuff is enforced on the way in (write), should I avoid enforcing it on the way out (read)? And if so, how? If not, why not?
- In case the database does have incorrectly stored data, it will be cleaned and misrepresented in my frontend.
- If I change the requirements and update the cleaning code, see 1.
I am saving objects that represent database fields. So I am storing their datatype (varchar, decimal, integer, etc), size ("25" or "9,2", etc), default values, nullability, etc.
So the object
name attribute must conform to MySQL naming rules (cannot start with a number) plus some rules I've added just to simplify things (only numbers, ascii letters, and "_"). This will be used as a column name in tables.
The "size" needs to match the data type, as well.
This is all PHP and MySQL running on Ubuntu.