「Php/pimple」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
|||
| 行14: | 行14: | ||
==サンプル== | ==サンプル== | ||
| − | $ vi | + | $ vi ServiceInterface.php |
| + | namespace App\Services; | ||
| + | interface ServiceInterface | ||
| + | { | ||
| + | public function doSomething(); | ||
| + | } | ||
| + | |||
| + | $ vi Service.php | ||
| + | namespace App\Services; | ||
| + | class Service implements ServiceInterface | ||
| + | { | ||
| + | public function doSomething() | ||
| + | { | ||
| + | return "hoge"; | ||
| + | } | ||
| + | } | ||
| + | $ vi PimpleexaController.php | ||
use Pimple\Container; | use Pimple\Container; | ||
use App\Services\Service; | use App\Services\Service; | ||
| 行28: | 行44: | ||
}; | }; | ||
$service = $container['service']; | $service = $container['service']; | ||
| − | echo $service->doSomething(); | + | echo $service->doSomething(); // hoge |
| + | } | ||
| + | } | ||
2017年9月9日 (土) 22:23時点における版
pimpleとは
DIコンテナのphpライブラリ
composerによるインストール
$ vi composer.json
{
"require": {
"pimple/pimple": "v3.*.*"
}
}
>Installing pimple/pimple (v3.2.2)
$ php composer.phar install
サンプル
$ vi ServiceInterface.php
namespace App\Services;
interface ServiceInterface
{
public function doSomething();
}
$ vi Service.php
namespace App\Services;
class Service implements ServiceInterface
{
public function doSomething()
{
return "hoge";
}
}
$ vi PimpleexaController.php
use Pimple\Container;
use App\Services\Service;
class PimpleexaController extends Controller
{
public function index()
{
$container = new Container();
$container['service'] = function ($container) {
$service = new Service();
return $service;
};
$service = $container['service'];
echo $service->doSomething(); // hoge
}
}
