LoginSignup
8
2

More than 3 years have passed since last update.

Composerでautoload.phpを作成する

Last updated at Posted at 2019-05-19

Composerでオートローダを作成する方法

ディレクトリ構成

composer.json
src/
  index.php
  bar/
    Hoge.php   ... foo\bar\Hoge クラス
composer.json
{
    "autoload": {
        "psr-4": {
            "foo\\": "src"
        }
    }
}

*composer.jsonの記述は場合によってことなります。
https://qiita.com/suin/items/b31e3c5bff15c821ccaa

コマンドラインでcomposer.jsonが置いてあるディレクトリに移動し
コマンドを実行

composer dump-autoload

vendorが生成される

composer.json
vendor/
  composer/
  autoload.php
src/
  index.php
  bar/
    Hoge.php   ... foo\bar\Hoge クラス

autoload.phpを読み込む

index.php
<?php
use foo\bar\Hoge;

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

参考文献

Composerのautoloadの書き方早見表

PHP開発でComposerを使わないなんてありえない!基礎編

8
2
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
8
2