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

davy_yg's avatar
Level 27

Namespace

What is namespace for in php? What about in laravel?

Is it to address the location of the file?

I have been reading this but do not understand it: https://www.php.net/manual/en/language.namespaces.rationale.php

0 likes
8 replies
jlrdw's avatar

Because of name spacing the correct file is able to be determined.

Just look at how the use statements in laravel determine the correct file to pull in.

You may have two files named Request, name spacing pulls in the one you want.

1 like
davy_yg's avatar
Level 27

use statement is to import other class by addressing the location.

Namespace to address the current file location?

jlrdw's avatar

Those other files also have a namespace so will the current file.

Do you know how your models are named spaced then you can have the use statement to use them in the controller.

1 like
davy_yg's avatar
Level 27

So the path that I write on the namespace is exactly the same as the path when I use (import) the class?

jlrdw's avatar
jlrdw
Best Answer
Level 75

Yes basically.

If you have this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'flight_id';
}

A flight model.

To use in a controller

Use app\Flight;

The app namespace and Flight within the namespace.

I'm sorry I can't explain it any better than that.

Cronix's avatar

So the path that I write on the namespace is exactly the same as the path when I use (import) the class?

"It depends." lol...as always

It depends on the location of the file you're talking about, as well as the location of the class(es) you're importing.

Namespaces are a way to separate code and be able to reuse class names. Imagine if you had a class that you called MySuperDuperClass, and you found a package that you really wanted to use, but that package also had a class named MySuperDuperClass. How would you tell your app which one was which, and how to load them? That's why namespaces exist.

So when you create a class in laravel (let's skip making a package, you're probably not), you tell it the namespace, which is the directory structure that leads up to that file starting from the app directory since that's where we put our custom files for laravel. That app dir's namespace is App.

So if you created a new class within the app directory, you'd give it a namespace App; declaration. If you created another directory within the app directory and named it Models, like /app/Models/, then any class that you create in that directory would have namespace App\Models; declaration, and on it goes.

Now let's say you created a class named Bank in the Models directory, like /app/Models/Bank.php, and you wanted to use that class in a different directory, like maybe you have a /app/Http/Controllers/MyController.php controller file and wanted to use that model. You'd import it following that same directory structure except add on the class/model name after it, like use App\Models\Bank;. Then whenever you reference the Bank model, it will load the model from that directory. This allows you to have multiple Bank classes in multiple directories and use them. That sounds crazy in this example, but maybe you find a package that also uses a Bank class. Without namespaces you wouldn't be able to do that. Before namespaces existed, every classname needed to be unique. It was much harder to have packages and stuff and people would name the classnames crazy stuff so that they'd be unique, instead of just readable, like Bank.

1 like
jlrdw's avatar

Which is what I meant when I said

Because of name spacing the correct file is able to be determined.

Yes you can have another class named Flight within a myapp namespace.

To use that class

use myapp\Flight;
// it's
use  namespace\class;

So again, Because of name spacing the correct file is able to be determined. I hope that was not hard to understand. I just can't explain it any better than that so sorry.

Please or to participate in this conversation.