「Php/composer/psr-4」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
(→サンプル) |
||
| 行15: | 行15: | ||
以下のようにrequire_once不要で読み込める | 以下のようにrequire_once不要で読み込める | ||
| − | src/ | + | src/MainService.php |
namespace myapp; | namespace myapp; | ||
| − | class | + | class MainService |
{ | { | ||
public function execLogic() | public function execLogic() | ||
{ | { | ||
| − | $sub = new | + | $sub = new SubService(); |
$sub->execLogic(); | $sub->execLogic(); | ||
} | } | ||
} | } | ||
| − | src/ | + | src/SubService.php |
namespace myapp; | namespace myapp; | ||
| − | class | + | class SubService |
{ | { | ||
public function execLogic() | public function execLogic() | ||
2017年11月8日 (水) 00:27時点における版
psr-4とは
クラスをオートローディングするための仕様
composer.jsonに以下追加
"autoload": {
"psr-4": {
"myapp\\": "src/"
}
}
composer更新
$ composer update
サンプル
以下のようにrequire_once不要で読み込める
src/MainService.php
namespace myapp;
class MainService
{
public function execLogic()
{
$sub = new SubService();
$sub->execLogic();
}
}
src/SubService.php
namespace myapp;
class SubService
{
public function execLogic()
{
}
}
