r/PHPhelp • u/CodingInTheCold • Nov 26 '19
Should I learn design patterns?
I've been learning/using php with frameworks like codeigniter, and wordpress, else just procedurally. I understand the syntax of OOP; classes, properties, access modifiers, and inheritance, but not really the any OOP design patterns (unless inheritance is counted).
Last week I got an interview for a trainee-ship and had to write a small amount of php (specifically) without a framework. First was to create a form that allows users to register their email address in MySQL. The other was to read an RSS feed and prepare JSON data to be fetched by JS on the front-end.
I kind of feel I needed to do this in OOP, but wasn't really sure if I needed to use a certain design pattern. I ended up just doing it procedurally, but was wondering how the professionals would have implemented this.
So if you had this assignment, how would you have implemented it? The procedural way, simple OOP - like a class and an instance, or OOP with some design pattern?
Is it a really bad thing that I don't have a grasp of basic design patterns yet?
Thanks.
2
u/tezvi Nov 26 '19
Short answer: yes Not trying to be funny but design patterns are essential knowledge when designing your code efficiently. You should understand it well.
2
u/nixicorn Nov 26 '19
I agree with everyone here design patterns are worth your time. I would suggest picking up the classic book Design Patterns by "the Gang of Four". I think it is still the best text I have read on the subject. Its a great reference I use frequently and keep near my desk at work. I don't always use a design pattern but when I do I reference that book.
1
u/fingerofchicken Nov 26 '19
You should learn them. They're useful approaches to common problems. But you shouldn't try and shoehorn them into every situation.
I wouldn't worry about feeling like you "should" do OOP. OOP is a convention. It's good for some situations. It's not for others. But it's not inherently better at everything.
1
u/MUK99 Nov 26 '19
Since ive learned them myself I have build much more lasting projects, easier to maintain, logical structure etc etc
1
u/nealio82 Nov 26 '19
The GoF book is a great reference, but is kind-of tough to get to understand if you're quite new to this kind of thing (especially if you're not familiar with UML). I would suggest the series at https://laracasts.com/series/design-patterns-in-php as a starter (you can always sign up, watch the videos, then cancel your subscription...), and https://sourcemaking.com/design_patterns as an online reference to most of the design patterns you're going to need as a PHP developer.
Honestly though, the Laracasts series on these patterns is excellent. That would be my starting point if I were you.
Is it a really bad thing that I don't have a grasp of basic design patterns yet?
Absolutely not. I'd been in this game for a few years before touched design patterns, or even SOLID (I failed a job interview miserably, and the interview feedback was the first time I'd heard about SOLID or proper OOP approaches). The fact you're asking these questions means you're already making progress.
So if you had this assignment, how would you have implemented it? The procedural way, simple OOP - like a class and an instance, or OOP with some design pattern?
Depends on the complexity, or how 'important' it is in a business sense. If it's a one-off or internal admin script (eg, the RSS reader is just showing some new items on an intranet or something), then procedural would be fine as long as it's well tested and validated.
On the other hand, if important business decisions are to be made on the code (eg, the RSS feed is parsing stock market updates or something like that), or if there are reusable components, then it'd be better to use an OOP approach
They're probably looking at your ability to provide (and explain) appropriate implementation decisions. If it's some throwaway or unimportant code, just shit it out (http://rosstuck.com/the-everybody-poops-rule) and don't waste valuable time over-engineering. If it's code that's going to hang around for a while, spend some time cleaning it.
1
Nov 26 '19
Kinda. I did a degree in CS so I had to learn them as part of the curriculum. It did help me understand some code a bit more (particularly any library that embraces them). I'll fully admit though that outside of an academic scenario, I've never coded anything using a design pattern.
As for your specific example - I'd probably have done it procedurally too. To me PHP just feels more "natural" used that way. I do a good amount of C# development too and fully embrace OOP there, but 99% of the time I don't bother in PHP.
1
2
u/wadesherman Nov 26 '19 edited Nov 26 '19
Design patterns are really just templates to use in OOP to help make sure that you have the correct amount of abstraction in your code.
Your examples don’t really seem complex enough to warrant any of the most common patterns. If the first problem was “develop a code that takes in data from any of a number of forms and puts it into the corresponding database table” you would probably want to employ a few design patterns to make sure your project is designed most efficiently
Edit: Yes