0

This question is more a design oriented question than a code specific question. I am new to PHP and I am planning to use PDO as a data access layer. Say for instance I have a class called CITY. Now if I need to create an instance of this class, what is the best technique.

  • Should have a singleton DB access class which is used to write and read data from the db layer.
  • OR should I delegate it to the individual class object. For example if I invoke city.save() (city is a class), then the city class will handle the saving of that city object's data into the database.

Excuse my ignorance but i have a java background and therefore trying to understand what is the best design principle for data management when using php.

2 Answers 2

1

I'm afraid there is no definitive answer, it depends on the project at hand.

I've been pondering the same question as yours for my own project. In search for an answer I've been looking at some PHP books and according to George Schlossnagle in 'Advanced PHP Programming' there are three possible options:

1) Using the Active Record Pattern: the class takes care of saving it's data into the database. Very useful if the class has a simple correspondence with individual table rows, but harder to use if your class depends on data from multiple tables

2) Using the Mapper pattern: One class (the Mapper) deals with the database and the other class for example the User class deals only with the User object itself. Basically the Mapper maps the User class to the database structure.

3) A combination of both, which Schlossnagle calls 'Integrated Mapper'

Personally I would have one class dealing with the mere basics of PDO: connecting with the database and performing a query. Basically making sure you only have to use your DB credentials in one place. All object specific queries I would deal with them in the object's class, since the data of the object is in my view strongly linked with the database structure, so if the class structure changes the database structure probably needs to accommodate those changes as well. And vice versa. So I'm leaning towards using a combination of both Active Record and Mapper pattern.

I hope this helps you in formulating your own solution.

-2

Java

Welcome to a, in my opinion, less confusing and less redundant language. A lot of what you know from Java can be transferred to PHP, the theories and design patterns will all be the same, as it is for most, if not all, languages.

"Should have a singleton DB access class which is used to write and read data from the db layer."

Probably the most "secure" method. I've not done it myself, mostly because I've not had the chance to play around with PDO or MYSQL yet, but I have read quite a bit on it and this seems to be the preferred method. It is common practice to limit instantiation of DB classes to control its flow, and a singleton class would be perfect for this.

Here is the pertinent part from a decent article on the subject. This is the second page from the third part of a multipart series. If you are interested in the rest it should be linked in the "See Also" box. While this is relatively old, it should still be pertinent as I don't think the theory has changed. I hope this helps!

Your Answer

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