Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alchermd's avatar

Do you prefer helper functions over Facades/DI?

To be precise, I'm talking about something like:

public function store(Request $request)
{
    $request->validate([/** ... */]);

    if (Auth::check()) {
        // ...
    }

    return view('foo.bar');
}

over something like:

public function store()
{
    request()->validate([/** ... */]);

    if (auth()->check()) {
        // ...
    }

    return view('foo.bar');
}

I used to think that the first one is more clear since Facades and/or injected dependencies are explicitly defined as parameters/imports. But lately I've been seeing that using helper functions results to more terse code, which kinda leads to lesser cognitive overhead when scanning a certain codebase.

What do you prefer?

0 likes
5 replies
GeorgeHanson's avatar

I generally prefer to use helper functions. In my opinion I find that having lots of facades can make the code harder to read - but it's really just a personal preference.

IIRC helper functions just resolve the classes from the container anyway, so there isn't really much difference between using a helper function or a facade.

alchermd's avatar

@georgehanson

IIRC helper functions just resolve the classes from the container anyway, so there isn't really much difference between using a helper function or a facade.

My only gripe is that it messes up with autocompletion for PHPStorm. I hope there's an alternative way, like methods on the base Controller class so that we can do something like:

public function store()
{
   // This is already an option
   $this->validate();

    // Maps to auth()->check(), for example.
    if ($this->user()->isAuthenticated()) {
        // ...
    }

    // Akin to how Symfony does it.
    return $this->render('foo.bar');
}
martinbean's avatar

@alchermd I prefer façades (and then type-hinting where it makes sense).

I’d rather method calls look like they’re being done on objects (even if they do look like static method calls) rather than global functions.

alchermd's avatar

@MARTINBEAN - Oh yeah, that's exactly what I'm thinking of! With the trajectory of PHP becoming more Java-like, using global functions just feels out of place. I wouldn't have any issue with it if these functions need to be explicitly imported, though.

Please or to participate in this conversation.