Data Access Layer, Best Practices

Posted by labratmatt on Stack Overflow See other posts from Stack Overflow or by labratmatt
Published on 2010-05-19T19:09:58Z Indexed on 2010/05/19 19:10 UTC
Read the original article Hit count: 380

I'm looking for input on the best way to refactor the data access layer (DAL) in my PHP based web app. I follow an MVC pattern: PHP/HTML/CSS/etc. views on the front end, PHP controllers/services in the middle, and a PHP DAL sitting on top of a relational database in the model. Pretty standard stuff. Things are working fine, but my DAL is getting large (codesmell?) and becoming a bit unwieldy.

My DAL contains almost all of the logic to interface with my database and is full of functions that look like this:

function getUser($user_id) {
   $statement = "select id, name from users where user_id=:user_id";
   PDO builds statement and fetchs results as an array
   return $array_of_results_generated_by_PDO_fetch_method;
}

Notes:

  • The logic in my controller only interacts with the model using functions like the above in the DAL
  • I am not using a framework (I'm of the opinion that PHP is a templating language and there's no need to inject complexity via a framework)
  • I generally use PHP as a procedural language and tend to shy away from its OOP approach (I enjoy OOP development but prefer to keep that complexity out of PHP)

What approaches have you taken when your DAL has reached this point? Do I have a fundamental design problem? Do I simply need to chop my DAL into a number of smaller files (logically divide it)? Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about best-practices