Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iamjaime's avatar

When to use PHP design Patterns

I have been coding for many years now, and I have been trying my hardest to keep my code clean.

I purchased Laravel Spark just to see an example of how @TaylorOtwell codes because i find it really interesting how people code with such cleanliness.

My question is, does anyone know how I can master PHP design patterns?

My main problem is that I don't know when is the right time to use a specific design pattern.

How can I perfect that skill of choosing the right design patterns?

0 likes
3 replies
jlrdw's avatar

How can I perfect that skill of choosing the right design patterns?

At times you could ask 5 experts which is best, and you might get 5 different answers.

I'd say write clean code yes, and just browse good quality github applications and draw ideas here.

A design pattern to me is basically certain code here, put that class there, etc.

Some like the repository pattern, some just go for fat controllers.

Now for a secret: The cpu cares less where the code comes from.

It will run the same whether it came from a repository pattern, or from a plump controller, or even vanilla php spaghetti code.

OOP, patterns, etc are for humans for readability, a compiler and the cpu does not care.

But I like readable clean code.

iamjaime's avatar

Yes, i'm sure the CPU doesn't care however when you have readable code that follows certain principles such as S.O.L.I.D it allows for code to be extensible easily, completely decoupled and less of a headache when it comes to maintaining the code base or releasing updates.

The difficult part is knowing when to choose what design pattern.

jlrdw's avatar

The thing about some of these patterns are:

  • They have been around a long time
  • Many are just common sense
  • Java JSP Servlets and EJB's use all along

I came from java, but I never heard all this design pattern talk til php frameworks got real popular. Yet the principals have been used for years. Just seems more talk about them on php framework forums.

Just use them and common sense.

Like if you use good laravel conventions are you using a good pattern, yes or no?

Mostly yes, but consider

Both, the best convention would be normal queries and php code, why, well if laravel was no more it would be easier to re-code a large application. But that's another story.

But other conventions are good in laravel. Me, if a query gets complex, or I have to use a raw statement I use normal PDO instead with getPdo().

But just an example.

So learn patterns, but good programming common sense is in effect using a pattern.

And consider, if one pattern is better than another, why is there more than one pattern?

You could paradox yourself in a corner.

Please or to participate in this conversation.