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.
1
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:
(from: http://www.osnews.com/story/19266/WTFs_m)
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:
- PHP 5.2: Zend_Db (ZF1)
- PHP 5.3: ZendDb (ZF2)
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.
I haven’t worked in a PHP project for long now.
But I remember a few data abstraction layers for the PHP:
- PDO
- PEAR DB (obsolete)
- PEAR MDB2
Beside these, PHP frameworks feature helpers and data layers (for example CodeIgniter database library).