1

I am currently reviewing a code base and noticed that a majority of the calls (along with DB connections) are just buried inside the PHP scripts. I would have assumed that like other languages they would have developed some sort of data access layer (Like I would do in .Net or Java) for all of the communication to the DB (or implemented MVC, etc). Is this still a common pattern in PHP or is there alternative methodologies/patterns for this technology? I am just trying to understand why the subs would have developed it this way.

Any insight/info on how experienced developers design an approach data access in PHP would be very much appreciated.

  • There's a lot of out of date and/or bad tutorials online that still show data access using the mysql_* functions, so even to this day I'm seeing new projects started using them. But PDO (PHP Data Objects) has been around for a while, and is enabled by default in most PHP builds. – GrandmasterB Oct 17 '12 at 18:33
7

It is pretty common in PHP Projects to have some kind of Data Abstraction or Data Access Layer. Simply because of DRY Principles and cleaner error handling. Experienced Programmers don't like to repeat themselves over and over again and they seperate domain logic from data manipulation as much as possible.

There are lots of very good Frameworks and components out there to help with Data Abstraction or even Object Relational Mapping, see this Post at SO.

You might experience what lots of other code reviewers experienced before:

WTFs per Minute

(from: http://www.osnews.com/story/19266/WTFs_m)

  • Jan thanks! The depiction on the right is exactly where I was when doing the review. – scarpacci Oct 17 '12 at 16:16
1

It's still very common. Please do not invent the wheel - here are two great libraries you can use:

Zend Framework's Db component:

Doctrine's DBAL component

PDO is a nice abstraction layer, but it's lower level than these other two. I'd recommend Zend Db over Doctrine DBAL, unless you want an ORM, then I highly recommend the Doctrine DBAL/ORM route.

1

I haven't worked in a PHP project for long now.

But I remember a few data abstraction layers for the PHP:

Beside these, PHP frameworks feature helpers and data layers (for example CodeIgniter database library).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.