Code Monkey home page Code Monkey logo

Comments (6)

hubertnnn avatar hubertnnn commented on July 30, 2024 2

@alexkuc
Not true.
In fact, using facades is the easiest way for testing since facades where designed for easy testing in mind.

Also if you are using the new Octane/Swoole system instead of php-fpm,
then you should avoid constructor injection completely, since it might leak data between requests.

So in current version its best to use Facades everywhere, unless you need support for non-laravel projects.

from laravel-best-practices.

zlodes avatar zlodes commented on July 30, 2024 1

Facades must die.

If you use facades (e.g. Log or DB) you cannot write clear Unit test.
Always use DI and then you may pass mocks into class you testing.

Remember, Unit test must extend TestCase from PHPUnit namespace.

from laravel-best-practices.

N69S avatar N69S commented on July 30, 2024

a) Constructor injection:
i dont know. Personally, i only use it when there is no other solution and to keep my classes testable by avoiding hidden dependencies.
b) Method injection
Easily avoidable by using eloquent relations. or limit it to repositories (if you use them).
c) Facades
To be used as a tool for singleton and factory pattern
d) Global helper functions
If the function is totally independent from any other class and require only it's parameters. example:
function sum($a, $b) { return $a + $b; }
For Laravel global helpers: Use with caution, not everything is to be used anywhere or used at all. like you dont call request() helper in model level

from laravel-best-practices.

hubertnnn avatar hubertnnn commented on July 30, 2024

I personally use those depending on context:

In libraries

  • I use Constructor injection when possible (so I can use the library also without Laravel)
  • If not possible and I know the library will be Laravel only (eg. its using Eloquent) then I use facades and helpers
  • I never use method injection

In application code

  • I use Method injection only in Controllers
  • In other places I use helpers and Facades
  • I avoid constructor injection (performance reasons and to reduce amount of code and pushing around variables)

from laravel-best-practices.

alexkuc avatar alexkuc commented on July 30, 2024

I think constructor or method injections lead to code that is easier to test. Having a facade or global helper within the class itself leads to tightly coupled code. Making it harder to test, refactor, etc. (doesn't mean impossible, but harder).

from laravel-best-practices.

alexkuc avatar alexkuc commented on July 30, 2024

@zlodes

Facades must die.

I wouldn't go this harsh. I believe helpers and facades in Laravel were made for RAD and POC code. When you have clear business and technical requirements, it must makes sense to spend time writer leaner and cleaner code. But when you are improvising or dealing with high level of uncertainty, I think that would lead to waste. Imho.

from laravel-best-practices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.