1
2

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 5 years have passed since last update.

Composerとautoloadの小さなメモ

Posted at

参考

これの通り
これで理解できるComposerを使ったオートロード | Reffect Blog - http://reffect.co.jp/blog/post/composer-autoload

メモ

srcディレクトリの下にクラスファイルを作成

src/User.php
<?php
namespace Kuredev;
class User{
    private $name;
    public function __construct(string $name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }
}

ルートディレクトリにcomposer.json作成
名前空間Kuredev(=srcディレクトリ配下から)

composer.json
{
  "autoload": {
    "psr-4": {
      "Kuredev\\": "src"
    }
  }
}

以下実行

$ composer dump-autoload

以下の通り、vendorディレクトリ配下にautoload関連のファイルが作成される

ScreenClip.png

以下のように venor/autoload.php を読み込み、名前空間Kuredevのクラスを使える。

test.php
<?php
require('vendor/autoload.php');
$user = new Kuredev\User("kure");
echo $user->getName();
1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?