I’m researching ways to create a OO-model/repository-layer using PHP’s PDO. My idea was to create model classes that represent a domain object and a repository class for each model that has the responsibility of:
- Saving to the database by taking a model as argument and mapping the objects properties to the VALUES of the SQL-query.
- Reading from the database by return an instance of a model in a similar fashion.
I was inspired from a course in OOP in which we used .NET, EF and LINQ for the Labs.
I asked a question on SA about how this could be achived on SA. I got flamed because is is supposedly bad practise.
I don’t see why this is the case.
2
There are already libraries that do what you want available for PHP. It’s called O/RM or Object/Relational Mapping. Not being a PHP programmer myself, I’ll relegate suggestions to the experts
Funny, that’s not the first time I’ve seen people arguing against using objects to represent database records in dynamic Object-Oriented languages. I recently read a Python thread arguing to just access the data record directly. Frightening to say the least.
5