One new feature of Laravel 5.4 are so-called “Automatic Facades”. My colleague was expecting my reaction when he sends me the link to the blog entry announcing this feature (I’m not linking it intentionally) with a thievish smile.
As Laravel’s documentation puts it, “Facades provide a static interface to classes that are available in the application’s service container”. In other words, you can access any class almost everywhere without hassling about nasty things like dependency injection. Hm? Do I hear Singleton somewhere?
So far it was required to add Facade classes in order to make this work. It seems even this little barrier has fallen now.
Laravel’s Facade, in my opinion, is one of the worst features a PHP framework offers nowadays. After times of Zend Framework 1 and the infamous Registry (I abused it too), when IoC finally arrived in the PHP world, it somehow seemed to me as quite a setback when Laravel became more and more popular with its Facade feature (it’s not even the Facade design pattern anyway).
1. It’s tempting
Making classes available in the code everywhere is tempting inexperienced (and experienced) developers to use the facade everywhere, without thinking if this might be a good idea or not. I’ve seen things! I even found Facade in configuration files. IN CONFIG FILES WTF!!1
If you don’t pay close attention, your classes soon will violate the single responsibility principle, simply because you don’t really notice how many dependencies you suddenly use.
2. It binds your code
In my opinion, code should be bound as less to the underlying framework as possible. I totally get the advantages of ORMs and such, I’m not opting against packages etc., but binding your code too close to a framework can cause problems if you want to use your business logic over longer terms.
But well, I guess if you are willing to use non-namespaced helper functions everywhere in your code (another Laravel feature I “deeply adore”), you don’t really care about this either.
3. It requires more overhead to test
“But Facades are more testable than traditional static methods!” I hear the Artisans yell. Yeah, sure, that is true.
What a benefit!
It just requires bootstrapping of Laravel for every test, making unit tests significantly slower. Your tests will become more complex and thus require more maintenance, as you will need to mock more dependencies which are all required to make one test work.
4. It’s actually not that convenient
Any modern IDE provides Code completion, so I can see in a second which methods a class offers. With Facade, I need an additional package that parses the Facades and generates documentation files that the IDE can parse for the Code completion.
When stepping through the code during debugging it annoys me pretty much to go through the facade over and over again. It’s just so unnecessary…
Probably both IDE and Debuggers are not so commonly used amongst Artisans?
5. It’s not more expressive
I really don’t get why
is more “expressive” than
Somebody needs to explain!
To be fair, even the official Laravel documentation states that Facade should not be used when building a third-party package, and that it bears some of the above mentioned risks. So Taylor is aware of it. But then why is this feature enforced in the first place anyway? I think it’s because Laravel claims to be so “expressive” and “beautiful” in it’s syntax that everybody accepts the downsides.
Decide for yourself if it is worth it.
Probably I’m just missing the point here? Why do you think Laravel’s Facade should be used rather than Dependency Injection?