Sheed
Sheed

Reputation: 619

How does namespace link to folders?

(I did look into how namespaces work, but people explain it from a functional PoV and not physical)

I have a problem with Laravel but that's not the real problem: in a blog/app folder I have the following code

<?php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
//...
?>

For example the third file referred to is in \blog\vendor\laravel\framework\src\Illuminate\Foundation\Auth

How the hell does it even figure out that part blog\vendor\laravel\framework\src ?

I have tried "right-click go to declaration" with PhpStorm and it linked me to the right file. How did it know? Using WampServer I opened app not vendor...

Note: the reason I'm asking this is because I get Class 'Illuminate\Foundation\Auth\User' not found in[...] when I try to open that file... (even though PhpStorm found it!)

Upvotes: 0

Views: 57

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53563

There is no definitive way to manage this relationship between namespace and physical file, they are not technically related. However, in order to alleviate the issue you're noting, the PSR-4 standard was created, which defines guidelines to tie namespaces to file system files.

In this particular case, that PSR-4 mapping is maintained automatically by Composer. The Illuminate package should have a composer.json that tells Composer where it can find files for the Illuminate namespace. When you run composer install, Composer scans through all the dependent composer.json files and builds vendor/autoload.php from all those dependencies. So all you have to do is include vendor/autoload.php and you magically get access to everything in your dependencies list.

Upvotes: 3

Related Questions