このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
basic-library:league-container:3.3 [2020/04/19 12:00] y2sunlight [【テスト1】手動によるコンストラクター・インジェクション] |
basic-library:league-container:3.3 [2020/04/19 16:38] y2sunlight |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > TODO: | ||
- | |||
- | ----- | ||
- | |||
====== DIコンテナー - League/ | ====== DIコンテナー - League/ | ||
Version 3.3 ([[https:// | Version 3.3 ([[https:// | ||
行 165: | 行 161: | ||
D: | D: | ||
D: | D: | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== テスト2 ==== | ||
+ | |||
+ | DIコンテナ― によるコンストラクター・インジェクションの例です。依存関係をクラスコンストラクターに渡すことは、依存性注入の最も簡単な方法です。League/ | ||
+ | |||
+ | <code php index2.php> | ||
+ | <?php declare(strict_types=1); | ||
+ | require __DIR__.'/ | ||
+ | |||
+ | // ComposerのAutoloadの代替 | ||
+ | spl_autoload_register(function ($class) | ||
+ | { | ||
+ | require __DIR__.'/ | ||
+ | }); | ||
+ | |||
+ | // | ||
+ | // DIコンテナによるコンストラクター・インジェクション | ||
+ | // | ||
+ | $container = new League\Container\Container; | ||
+ | |||
+ | $container | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | ; | ||
+ | $container-> | ||
+ | $container-> | ||
+ | |||
+ | $foo = $container-> | ||
+ | |||
+ | var_dump($foo instanceof Acme\Foo); | ||
+ | var_dump($foo-> | ||
+ | var_dump($foo-> | ||
+ | </ | ||
+ | |||
+ | === 結果 === | ||
+ | < | ||
+ | D: | ||
+ | D: | ||
+ | D: | ||
</ | </ | ||
行 170: | 行 209: | ||
==== テスト3 ==== | ==== テスト3 ==== | ||
- | > TODO: | + | League/ |
+ | |||
+ | >Auto Wiring はデフォルトで無効になっているので、有効にするにはコンテナデリゲートとして '' | ||
<code php index3.php> | <code php index3.php> | ||
行 183: | 行 224: | ||
// | // | ||
- | // Auto Wiring による | + | // Auto Wiring によるコンストラクター・インジェクション |
// | // | ||
$container = new League\Container\Container; | $container = new League\Container\Container; | ||
- | // リフレクションコンテナーをデリゲートとして登録してAuto Wiringを有効にする | + | // ReflectionContainerをデリゲートとして登録してAuto Wiringを有効にする |
- | // (注)既定では、リフレクションコンテナーは、要求するたびに要求を解決します。 | + | // (注)デフォルトでは、ReflectionContainerは、要求するたびにそれを解決します。 |
$container-> | $container-> | ||
new League\Container\ReflectionContainer | new League\Container\ReflectionContainer | ||
行 210: | 行 251: | ||
==== テスト4 ==== | ==== テスト4 ==== | ||
- | > TODO: | + | デフォルトでは ReflectionContainer は、要求の度にそれを解決しようとします。ReflectionContainer でキャッシュ機能を有効にするには以下のようにcacheResolutions()を使用します。 |
<code php index4.php> | <code php index4.php> | ||
行 223: | 行 264: | ||
// | // | ||
- | // Auto Wiring による | + | // Auto Wiring によるコンストラクター・インジェクション |
// | // | ||
$container = new League\Container\Container; | $container = new League\Container\Container; | ||
- | // リフレクションコンテナーのキャッシュを有効にするに、以下のようにします | + | // ReflectionContainerのキャッシュを有効にするに、以下のようにします |
$container-> | $container-> | ||
(new League\Container\ReflectionContainer)-> | (new League\Container\ReflectionContainer)-> |