「Php/pimple」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→DLされたPimple) |
(→サンプル) |
||
行74: | 行74: | ||
} | } | ||
} | } | ||
+ | |||
+ | ==参考== | ||
+ | http://blog.a-way-out.net/blog/2015/08/31/your-dependency-injection-is-wrong-as-I-expected/ |
2017年9月9日 (土) 22:29時点における版
pimpleとは
DIコンテナのphpライブラリ
composerによるインストール
$ vi composer.json
{ "require": { "pimple/pimple": "v3.*.*" } }
>Installing pimple/pimple (v3.2.2)
$ php composer.phar install
DLされたPimple V3系
vendor/pimple/pimple/src/ └── Pimple ├── Container.php ├── Exception │ ├── ExpectedInvokableException.php │ ├── FrozenServiceException.php │ ├── InvalidServiceIdentifierException.php │ └── UnknownIdentifierException.php ├── Psr11 │ ├── Container.php │ └── ServiceLocator.php ├── ServiceIterator.php ├── ServiceProviderInterface.php └── Tests ├── Fixtures │ ├── Invokable.php │ ├── NonInvokable.php │ ├── PimpleServiceProvider.php │ └── Service.php ├── PimpleServiceProviderInterfaceTest.php ├── PimpleTest.php ├── Psr11 │ ├── ContainerTest.php │ └── ServiceLocatorTest.php └── ServiceIteratorTest.php
サンプル
$ 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 } }
参考
http://blog.a-way-out.net/blog/2015/08/31/your-dependency-injection-is-wrong-as-I-expected/