Everything About The "Use" Keyword In PHP

profile picture

Everything About The "Use" Keyword In PHP

You surely encountered this use keyword in php countless times, in different situations. Isn't that a bit funny that the same exact keyword is used in different contexts, for very different reasons?

Let's review all places where this keyword ´use´d (pun intended).

Import Namespaced Classes

All modern PHP framework nowadays adopted the namespace convention, meaning a typical controller will look like this:

namespace App\Http\Controllers;

use App\Article;

class ArticleController extends Controller
    public function index()
        $articles = Article::where('published', true)->latest()->paginate(20);
        return view('article.index', compact('articles'));

If you don't import the Article class from its full namespace App\Article with the use keyword, PHP won't understand what exactly is Article.

Insert Traits

Wanna use Traits to share some code between multiple classes ? Easy! Let's look at this very simple example:

namespace App;

use App\Traits\Sluggable;

class Article
	use Sluggable;
	public function getSlug()

The slugify method is defined in the Sluggable Trait. But because you imported that trait inside the Article class, you are now able to use this method as if it was defined in the Article class itself.

Note that the use keyword is used inside the class brackets. This is a main difference with the first example.

Outside the class = import a class

Inside the class = import a trait

Inherit Variables in Closures

I'm going to take Laravel example here again. Let's take a look at this code:

public function search(Request $request)
    return Post::whereHas('categories', function ($query) {
	    $query->where('name', $request->input('category_name_search_input'));

In this search method we want to return all Posts that have a specific category name.

But this code won't work. In fact, it will blow up and return a Fatal Error saying that the $request is undefined. Hu, weird ?

Well that's quite logic actually, because the function we are passing in the whereHas() is a Closure. And close have their own scope, which mean they can't access variables that are defined beyond (outside) their scope.

As for Namespaced classes and Traits, we have to import variables into the scope of the closure so it can be used. And this is where the use keyword will help you.

Here is our working code:

public function search(Request $request)
    return Post::whereHas('categories', function ($query) user ($request) {
	    $query->where('name', $request->input('category_name_search_input'));

Now it works fine !

There you go, all the uses of the use keyword in php !


about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me


A weekly email with the latests articles

support my work

Start trading on binance
  • BTC



  • ETH



  • Monero



(adsbygoogle = window.adsbygoogle || []).push({});

2022 © My Dynamic Production SRL All rights Reserved.