views:

120

answers:

5

Hey everyone,

I read a book awhile back called PHP Design Patterns and Practice, and ever since then I have been using design patterns whenever I think they are needed. However it just occurred to me that maybe most companies do not use design patterns very often for PHP, or at all. What I was wondering is, do most companies use design patterns to help improve code flexibility? And if so, what are the best design patterns to learn for PHP?

Thanks for any help on this, Metropolis

Thanks for all of your great answers on this!
The conclusion I can take from this is: Patterns are used in almost all work places with PHP, and the most common ones need to be understood and memorized. Also, the MVC architectural pattern is very important and everyone will expect you to know it.

+1  A: 

Practically speaking, design patterns are good ideas should be implemented but are most important when you've got to normalize the skill level of multiple engineers working on the same systems. Consider the situation where you may have 3 or 4 engineers of varrying skill level working on the same project, touching each others files and interacting with code that they may not have written or know the background on. One way that design patterns and common practices help is to keep everyone on the same level and enforce commonality across the code base.

Nick Gerakines
Great point nick
Metropolis
+1  A: 

In my experience, the answer is: very often. Almost every place I've worked at uses design (and architectural) patterns. These are larger projects and it helps in implementation, maintenance and analysis to use known solutions to common problems.

You'll want to learn all the biggies (because you'll both use and run into them) starting with: Factory, Singleton, Observer, Strategy.

MVC is an architectural pattern that you should know well.

webbiedave
What is MVC webbie? And its great to know that this is a common thing...
Metropolis
MVC is a pattern for organising your business and view logic. See http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller for more information.
Pheter
Ah yes, I use that a lot, I just did not know what that acronym was. Thanks Pheter
Metropolis
+1  A: 

Probably the most commonly used design patterns are the singleton and the factory patterns. I've recently found the observer pattern extremely useful for triggering events when an object is changed in some way (similar to database triggers); and I find the Fluid Interface pattern particularly useful as well, allowing a chain of method calls. All of those are used regularly within my workplace.

Mark Baker
+1 for Fluid Interface.
Jeremy Kendall
While the singleton pattern is commonly used, I have found that it is often misused in PHP. It would be silly for me to say that you should never use it, but it is important that when implementing the singleton pattern one is aware that it is essentially a global. Numerous discussions on the issues of using globals and singletons can be found on SO.
Pheter
Agreed Pheter. I have found some instances where it is useful, but very seldom. I do however love the factory pattern. It took me awhile to realize how powerful that pattern is because at first glance it seems like it does not do anything to help.
Metropolis
singleton is often misused; but it does have its purpose and can be very valuable in the right situations, so it shouldn't ever be dismissed.
Mark Baker
+1  A: 

I can only speak from my experience, but most of the places I've worked have done a good job of learning and implementing design patterns/best practices in the workplace. Whether a company follows best practices or not is really a question of corporate/dev culture and the team's level of expertise.

As far as what patterns are best, you can never go wrong with dependency injection, one of my faves. IMHO, the best patterns to use are those that best fit the use case you're addressing.

Good luck!

Jeremy Kendall
A: 

For any non-trivial OO project design patterns are not an option, they are the only way to go. That is if you want to keep your sanity as the codebase evolves and expands. From what I've seen, companies that work on PHP eventually decide to invest in some sort of framework. Modern PHP frameworks are showcases for design patterns, especially the basics like Factory and Singleton. Properly used they can result in very extensible code.

The bad old days of include()-infested non-OO PHP4 code are thankfully behind us.

Manos Dilaverakis
we are always living in the bad days, we just don't know it yet :)
Anurag