Grzegorz Korba Profile picture
Aug 24 18 tweets 8 min read
There were many discussions if #Laravel's facades implement GoF's Facade Pattern, but I think it does not matter at this point - the team won't change naming convention anyway. Naming is not a problem, I see other issues with facades - a #PHP thread 🧵
1) They're basically magic 🪄 Some may see it as advantage, but I consider it as drawback. You don't execute exact code you're calling, but your call is proxied to some underlying service. It strictly couples your code with the framework, which handles it.
Facades' API has to be added as comments in PHPDoc (with `@Method`) which is error prone because it's easy to forget to update facade's phpdoc when underlying service (accessor) is changed. But even if autocompletion in IDE works, you just can't simply navigate to method's code.
There are of course tools like github.com/barryvdh/larav… which can solve some (or even all) of those problems, but for me it's not a great DX that you need external tool installed just to work with the code in reasonable way.
As a side note: the same problem is with #Laravel's helper functions. Of course, they simplify code (well, it's subjective) at the expense of very strict coupling with the framework. The main problem I see with it, is that people learn how to use Laravel, not how to write in PHP.
2) They break Single Responsibility Principle. #Laravel's facades act as static proxy to service locator, but at the same time... they provide testing API. In theory, you end up on production with mockable code - more below.
Since facades are used statically, they can't be replaced with other implementation during object initialisation. That's why base Facade class uses #Mockery & provides bunch of methods that allows testing. That overflows API for regular development - you need it only for tests. Image
You may think: "But it works! I can test my code" & you're somehow right. But since Facade uses Mockery, while #Laravel has it only in `require-dev`, you may end up with "malfunctioning" facades when you install with --no-dev option (e.g. on production). For me it's a code smell.
On the other hand, when you build/deploy improperly (#Composer install _without_ --no-dev), you may end up with mockable facades on production - testing code should not reach this stage. Add to it "debug on production" and here you go, looking for troubles 😅
3) Real-Time facades (laravel.com/docs/9.x/facad…): "you may treat any class in your application as if it was a facade"

I'm just amazed that someone had this idea and #Laravel officially suggests wrapping any service with magic facade 🤯
Again, some may say "It's awesome! It simplifies a lot!" and again they'll somehow right. But again: it breaks SOLID principles, covers explicit dependencies with magic, strictly couples code with #Laravel, teaches wrong (IMHO) practices.
You could use Dependency Injection approach and use facades' underlying services directly. They're listed on laravel.com/docs/9.x/facad…, most of them have Service Container Binding, that can be used for injecting.
I am NOT saying you have to drop facades entirely and use only DI - just choose wisely 😉 Do you want to be a PHP programmer or just Laravel Developer? Because if you follow programming principles, you won't have problems after switching to another framework.
This was discussed recently under @PovilasKorop's tweet: 🙂 It's not about #Symfony, but programming in general. #Laravel does not stop you from using good practices, it just provides its own conventions. It's your choice to use them or not.
@PovilasKorop Thank you for reading 🙏 I will appreciate your feedback and will happily discuss topics covered in this thread 😁

Whole 🧵👇
@PovilasKorop Actually it should be this tweet: 😅 @Twitter where's the edit button 😆
I would like to emphasise that "just Laravel developer" might be interpreted as something rude and pejorative, but it only was used to narrow programming scope. I don't think about Laravel developers as people worse in PHP and programming in general 🙂
FYI: I've decided to move this thread into blog post:

🔗 blog.codito.dev/2022/08/drawba…

It is extended with one more topic (autoloading), more visual examples and other little improvements 🙂

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Grzegorz Korba

Grzegorz Korba Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(