Help us understand the problem. What is going on with this article?

Composerとautoloadの小さなメモ

More than 1 year has passed since last update.

参考

これの通り
これで理解できる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();
kure
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした