Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?

More than 3 years have passed since last update.

@kure

Composerとautoloadの小さなメモ

参考

これの通り
これで理解できる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();
2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What is going on with this article?