0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】autoloadとnamespace を自分なりに解釈できたので

Posted at

はじめに

これは自分が忘れないようにするためのアウトプットとしての投稿です。

laravelを勉強中に
「そういえばファイルの読み込みでrequire使ってないな」
「どうやって読み込んでるんだ?」
となり
autoloadとnamespaceの学びに繋がったので
今回はそれを記録しようと思う。

概要

  • どうやってファイルを読み込んでいるのか?

    • autoloadという仕組みで読み込んでいた。
  • autoloadという仕組みによって自動で読み込まれると関数名やクラス名が競合してしまうよ

    • namaspace(名前空間)というクラスを階層的に整理するための仕組みを使う

autoload(オートロード)とは

  • ファイルを自動で読み込む仕組み
    • これでファイルを毎度requireして読み込まなくてもよくなりました。

laravelではどこでautoloadの仕組みが動いているのか?

laravel > public > index.php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/../vendor/autoload.php';

ここですね。
laravelアプリケーションの最初の入り口でそのファイルを読み込んでいました。

そしてそれはcomposerを利用してautoloadを動かしており(言葉あってるか?)

laravel > composer.json
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    }

ここで定義されています。
この記述ではapp配下のファイルを APPから始まるnamespaceとして設定しているそうです。

終わりに

知識荒削りですが少しずつその解釈に自信を持てるようになりたいですね。

自分の知識・解釈に自信ある人は尊敬します。

間違っている箇所などありましたら
編集リクエスト頂けたらと思います。

終わりです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?