I have a class that enforces database-friendly formatting for some of its properties in the __construct() method.

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?

Potential Problems

  1. In case the database does have incorrectly stored data, it will be cleaned and misrepresented in my frontend.
  2. If I change the requirements and update the cleaning code, see 1.

More info:

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.

  • @RobertHarvey good point. Question title is updated. May 5, 2016 at 23:02
  • 1
    Shouldn't __construct() throw instead of arbitrarily cleaning up the data? Can you trust the data from the database or not? Normally, when I read data from a database using a DTO, I want what's actually in the database, not what my business rules say is "correct." May 5, 2016 at 23:02
  • @RobertHarvey The interface allows users to be a little lazy about inputting the data. E.g. they can type "some column" and it will convert it to "some_column". I could have an intermediary perform the cleanup and have the main object just throw up if something goes wrong. But even then, it would be performing validation on read as well as write, right? May 5, 2016 at 23:04
  • 1
    @RobertHarvey Never mind. Just trying to figure out who should do what. Perhaps I should put the automatic data cleanups in javascript so the user sees exactly what they're going to be submitting. Then the object can validate itself in case anything bad gets through before saving, and not perform any transformations itself. May 5, 2016 at 23:09
  • 2
    That seems like a sensible approach. May 5, 2016 at 23:10

2 Answers 2


A good rule is to avoid having objects that are in a illegal state.

Therefore creating objects based on data from the database that is illegal (or indeed has become illegal because you have changed the rules) without anyone knowing about it would be bad.

It would of course also be bad if noone were able to start your system because one record somewhere were not legal, it could make the process of correcting those errors difficult.

So when loading data collect the errors and present them to the users, so that they can correct them. If you do not load all data at the start, make sure that the users understand that those errors are the errors found in this section, there might be others in other sections.

This would make it fairly easy to spot errors when the rules changes and to get them corrected as soon as possible.


If data is coming out of your database, then you can [usually] assume that it's correct. It wil have gone through a lot of checking to make it through in the first place, so re-checking it on the way out again is [usually] overkill.

Note the peppering of 'usually's in the above.

Not all of the data that gets into your database does so through applications and, even if they do, not all of those applications can be guaranteed to be as stringent in their checking as you might hope. It is, therefore, possible to get data into your database that can be considered "wrong". If you re-check data on the "way out" of the database, then you have no way of returning that errant data to the "outside World" where someone can do something about it.

Perhaps you need another "construct" method that takes an object returned from your database (RecordSet, DataReader, etc.) and populates the instance from that (no matter what it's given).

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.