PHP OOP Design Patterns | Factory Method
Вставка
- Опубліковано 24 сер 2024
- ℹ Factory method is a simple design pattern which enables you to delegate the creation of objects to a dedicated class...keeping your client code lean and observing the single responsibility principle.
🆓 Join garyclarke.tech for course DISCOUNTS and a FREE Docker + PHP course (worth $49)
www.garyclarke...
💻Source code here:
github.com/Gar...
👇Follow me on Social Media:
Twitter: / garyclarketech
Linkedin: / garyclarketech
💕Show support:
www.paypal.com...
Nice one Gary, keep them coming these intermediate videos. Thanks a lot for sharing.
Thanks, will do!
Great, as always, Gary!
Very information Gary. Thank you.
Nicely explained with good example.
Glad it was helpful!
Great video! Would love to see series on design patterns :)
Thank you for your work again !
My pleasure!
Thanks for sharing! Awesome video!
Thanks for watching!
Well explained, I like that you used real life example. What are the advantages of using Factory Method rather then simple if-else in the controller?
Ah thanks a lot...I appreciate your appreciation!
NIce Explanation, if i am not wrong decorator design pattern makes more sense for this example, modify the behaviour of free plan objects depending upon the plans. if no then please explain why factory pattern is more suitable.
nice video good job
Thank you for the great videos.
Question: When is it best to favour inheritance over composition?
Is it best to extend abstract classes or code to an interface? Are they any differences?
Inheritance over composition where composition definitely won't work. Only use inheritance when that is exactly what you need i.e. the subclass is a more custom version of the parent...not just if you want to add some more functionality.
Abstract v interface..if each subclass needs some functionality which will always be the same, then use a abstract...otherwise use an interface..or..quite commonly you will see that the abstract class implements an interface...these are not hard and fast rules..more like guidelines and it's stuff that you'll just pick up as you gain experience.
I cover all of the above quite a lot in Learn Object Oriented PHP:
www.garyclarke.tech/p/learn-object-oriented-php
Hope you check it out 👍
One more comment, while your tutorial is great and informational, you might also want to highlight sanitizing anything that gets passed to the POST and GET superglobals in a basic way. Just a suggestion.
Thanks Frank...that's a sensible suggestion.
Does the web server automatically make separate php files visible to others that are in the same namespace (so no need to use ‘include’ or ‘require’ keywords)? That is the only part of this tutorial that’s unclear for viewers working with an IDE and not launching a web server for testing.
You don't need a running web server for namespaces to work. You just need to have autoloading enabled.
When do you use double slashes like these: \\
If you are writing a namespace inside a string mainly because \ on its own means escape the next character...but you need a \ in a namespace
@@GaryClarkeTech wow thanks man. It makes sense now.
But in some wordpress tutorials I have also seen the name space, while being a string, use a single slash and also prefixed with a single slash like so:
'\App\Inc\Plans\Free'
It is like that in my spl_autoload function which I copied from somewhere. Im not sure why are single slashes used there or why a slash prefixes the namespace. Could you plz help me understand? Thanks for the amazing value in ur videos :D