mardi 26 août 2014

Laravel facades...misuse the term, they are proxies, ok?

The problem with the advent of Laravel and its popularity, is that it teaches wrong ideas to a huge amount of people.

Laravel facades are not facades. They definetely misuse the term.
What Laravel calls facade is just an opaque PHP magic method wrapper.
Said differently they're static proxies to service locators.

A proxy should be called a proxy, it is a pattern used elsewhere like in Java (RMI), Corba, etc.
You should also read about "dynamic proxy".

How are they different?
A facade hides something that takes a complex, multi-step operation, and simplifies it. So the interface (=public methods) is different: it is simplified, and 1 method call replaces several calls to one or many objects.

A proxy on the other hand has the exact same interface (meaning same list of public methods) of 1 proxied object, and usually add a feature (in example like logging method calls or usage of a network call).
(for some it could be confused with Decorator).

Learning programming "design patterns" is quite difficult, and this kind of framework is a danger from ths perspective.
So please take 2 minutes to learn the real facade design pattern: design_patterns/facade or here proxy-pattern.

I am glad some people know design patterns (@pmjones):  I could not resist to add my word to this discussion: about laravel facade.
Finally as I said there, maybe it is PHP magic methods are just plain evil for OO design.



2 commentaires:

  1. And what of cases where Laravel Facades ARE Facades (there are a few where they are not straight up proxies?).


    In Laravel, Facade is used as a noun, not an adjective. It's a "trade name" if you like, and it leans on the pre-existing english definition of the word, not the GoF pattern definition of the word.

    Can we please stop flogging this dead horse now?

    RépondreSupprimer
  2. I can't see any real Facades in the Laravel code.
    Anyway I understand people are free to name their stuff the way they want.
    At least there must exist traces online to debunk the misuse for those willing to go deeper, who search & learn.

    RépondreSupprimer