0

I have a large PHP project that I started a decade ago, and out of inertia, it's very functional in style. I've been using OOP for new features, and it's been making development a lot easier.

But I've been frequently running into a dilemma. Let's take a calendar as an example. Using objects makes total sense to model the database concept of a calendar category, a time range, the display model, etc. But when it comes to grabbing actual events out of the database, it feels more natural to me to just use the array of hashes that come out of the MySQL library. The overhead of creating hundreds, possibly thousands, of objects to model the results of a select query seems like overkill. It'd be nice to have them as objects, but at what cost?

What are the best PHP OOP practices for selecting potentially large numbers of rows from a database?

2
  • 2
    When it comes to taking rows out of a database, I tend to have a data access layer whose exclusive and sole remit is the ferrying data to and from the database. I tend to use ADOdb to do this and I tend to build DTOs, which are similar to Java beans in that all they are is encapsulators for the data. The overhead does not appear to be considerable and it is certainly not considerable enough to dissuade me from this approach. Commented Jan 5, 2012 at 14:48
  • 1
    @Wing, should post your comment as an answer. Commented Jan 5, 2012 at 15:52

1 Answer 1

1

My approach is somewhat similar I think to Wing's comment.

I have an object which I use throughout all projects that I can run a custom SQL query through and have it return an array; so it can be used in a way not too dissimilar to how I am (and you are) used to.

I build objects on top of that for particular projects where I need to run the same / similar queries a lot, I still make sure it returns something sensible (usually an array with field names as keys, why fix what isn't broken?)

It definitely makes a lot more sense to work with common queries in this way - so when you expand/change the data structure, you don't break 100's of pages, you just fix one object. Having the level above that you can run custom queries is most helpful for the 'one off' situations so you're not building new objects to handle every little thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.