DIとDIコンテナをサンプルコードを書いて理解する
DI(依存性の注入とよばれる)
これは正しくは必用なオブジェクトの注入と呼んだ方がいい。
ここでは、Gameをプレイする人が
DIのサンプルコード
code:php
<?php
// インターフェースを作成
interface GameSoftInterface
{
public function getTitle();
}
interface GamePlayerInterface
{
public function play();
}
interface TwitterClientInterface
{
public function post();
}
// GameSoftクラス
Class GameSoft implements GameSoftInterface
{
private $title;
public function __construct($title = null)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
// PS4クラス
class Ps4 implements GamePlayerInterface
{
private $gameSoft;
public function __construct(GameSoftInterface $gameSoft)
{
$this->gameSoft = $gameSoft;
}
public function play()
{
echo "PS4で{$this->gameSoft->getTitle()}をプレイします";
}
}
// TwitterClientクラス
class TwitterClient implements TwitterClientInterface
{
public function post()
{
echo "めっちゃ面白いなう!";
}
}
//クラス作成
class Player
{
private $gameSoft;
private $ps4;
private $twitter_client;
public function __construct(GameSoftInterface $gameSoft,GamePlayerInterface $ps4, TwitterClientInterface $twitter_client)
{
$this->gameSoft = $gameSoft;
$this->ps4 = $ps4;
$this->twitter_client = $twitter_client;
}
public function play()
{
echo "今日は{$this->gameSoft->getTitle()}を持ってきました! ";
echo "{$this->ps4->play()}";
}
public function tweet()
{
$this->twitter_client->post();
}
}
$ps4Soft =new GameSoft('SEKIRO');
$ps4 = new Ps4($ps4Soft);
$twitter_client = new TwitterClient();
$player = new Player($ps4Soft,$ps4,$twitter_client);
$player->play();
$player->tweet();
//今日はSEKIROを持ってきました! SEKIROをプレイしたい!!!めっちゃ面白いなう!
//Xbox360に入れ替えてみる
// Xbox360クラス
class Xbox360 implements GamePlayerInterface
{
private $gameSoft;
public function __construct(GameSoftInterface $gameSoft)
{
$this->gameSoft = $gameSoft;
}
public function play()
{
echo "Xbox360で{$this->gameSoft->getTitle()}をプレイします";
}
}
$xbox360Soft =new GameSoft('Halo3');
$xbox360 = new Xbox360($xbox360Soft);
$twitter_client = new TwitterClient();
$nikonama = new Player($xbox360Soft,$xbox360,$twitter_client);
$nikonama->play();
$nikonama->tweet();
DIコンテナ
依存の注入とインスタンスの作成を両方やってくれる仕組み
code:php
<?php
require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
use Pimple\Container;
// DIコンテナを生成
$container = new Container();
// 呼びだし方法を登録する
$container'gamesoft' = $container->factory(function($c){ });
$container'ps4' = $container->factory(function($c){ });
return new TwitterClient();
};
$container'player' = $container->factory(function($c){ });
// インターフェースを作成
interface GameSoftInterface
{
public function getTitle();
}
interface GamePlayerInterface
{
public function play();
}
interface TwitterClientInterface
{
public function post();
}
// GameSoftクラス
Class GameSoft implements GameSoftInterface
{
private $title;
public function __construct($title = null)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
// PS4クラス
class Ps4 implements GamePlayerInterface
{
private $gameSoft;
public function __construct(GameSoftInterface $gameSoft)
{
$this->gameSoft = $gameSoft;
}
public function play()
{
echo "PS4で{$this->gameSoft->getTitle()}をプレイします";
}
}
class TwitterClient implements TwitterClientInterface
{
public function post()
{
echo "めっちゃ面白いなう!";
}
}
//クラス作成
class Player
{
private $gameSpot;
private $ps4;
private $twitter_client;
public function __construct(GameSoftInterface $gameSoft,GamePlayerInterface $ps4, TwitterClientInterface $twitter_client)
{
$this->gameSoft = $gameSoft;
$this->ps4 = $ps4;
$this->twitter_client = $twitter_client;
}
public function play()
{
echo "今日は{$this->gameSoft->getTitle()}を持ってきました! ";
echo "{$this->ps4->play()}";
}
public function tweet()
{
$this->twitter_client->post();
}
}
// この時点で一気に必要なオブジェクトを生成して、playerインスタンスを生成する
$player->play();
$player->tweet();
参考文献