PHP Design patterns

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

I've been searching a lot today because I've come across someone telling me that in the next version of PHP they would be deprecating mysql_connect, which is what I currently use as my database class.

My database class is based on the the singleton design pattern, which in my searches I've come across people slamming that design pattern into the dirt. I went with this pattern along time ago because it reduces the database connection overhead by borrowing a connection if already setup instead of making a new one. This sped up a lot of my projects but now I'm in doubt about it.

I keep seeing people and site stating you should use PDO I'm very familiar with my class so if i went with PDO I'd most likely make pass through type functions to continue my current db usage style of calls.

SO ... Any thoughts?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

So I've started to take the plunge into changing my PHP database class, I know that PHP's PDO class would be fine on its own. My Database connection class was extremely easy to use and very flexible and there are several functions in my class that are in the PDO class. However Just switching to the PDO class would be the worse thing to do as far as backward compatibility. So I'm developing a wrapper class that will not break older projects, and maybe in later versions I'll phase out the wrapper class.

SO ... Thing is my class uses a function called in_quotes() and PDO has a function called quotes() but also has prepare(), from the documentation on PDO it looks like you can use quotes() if you don't use prepare() Which is fine because I can quickly make a pass through function in the wrapper class to handle it, but using prepare() looks like I'd have to change a lot more code to get it to function correctly. So the question I bring up to the PHP people on this site is this What is better the chicken or the eg ... I mean prepare or quotes?
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

LOL, well I like chicken more, as far as what came first, that would be a great question :)

Back to the design patterns, there are numerous design patterns out there and different design patterns are better for different situations. I understand why you might choose the singleton design pattern for your database class, and if its been working well for you I would say keep going with that. There is no "right" or "wrong" answer, sometimes it does come down to preferences too. If speed is the only thing you are basing the answer on, then there may be a right or wrong answer as to what design pattern might be the quickest. I would think the singleton design pattern might be good to prevent duplicate or numerous objects from being created that might use up memory or resources.

In my projects there are times that I have a script that needs to make more than one database connection, so a singleton design pattern is not used for my situation as I will often have a couple of objects that represent different databases and need both simultaneously.

For your last question, obviously the quote() is a very simple function that does its job, while prepare is much more complicated and also supports parameter markers. Again there is no right answer here, just depends on how much you want to do yourself, or if you want to use functions that handle quite a bit for you. For your situation and being backwards compatible though, you might just stick with the simple quote() function and do everything yourself as far as preparing any SQL statements. Then you would know exactly what is going on and would probably be easier to figure out how to do it and be backwards compatible at the same time.

Just my opinion ;)
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

That's what I was thinking as well, chicken is better! I was going to just use quotes but I wanted to pose the question anyway before I did to see others opinions, which may have influenced me on which direction to go. I've also contemplated ditching the Singleton design. Doing that I would just have to to a global replace on the sites that get the new class for the database connection. which isn't to hard.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

One thing I came across today was a singleton database manager wrapper which would allow multiple database connections if you used a singleton design. It looks like I could also use that for other design patterns, Though it is not needed with most other design patterns, If I did ditch the singleton design. I could use that wrapper for backward compatibility
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Sounds perfect then for you if you decide to change :)
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 475

Post 3+ Months Ago

Ok so I've finished writing/rewriting my database classes. My main database class I made into a basic PHP class style and ditched the singleton design. That class is using the the PHP PDO so it basically acts like a wrapper so I don't have to learn new functions to use the database. I then also have a singleton wrapper to provide 100% backward compatible code to my older projects. This wrapper will be phased out in my new projects.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 61 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.