DIã¨ã¯ï¼DIã³ã³ããã¨ã¯ï¼è©¦ãã¦ã¿ã(åç·¨)[PHP][DI]
DIã¯ãã¤ã¸ã ã¼DIã³ã³ããã¯ãã³ãªã ã¼ã¨ãã¯è²ã è´ãã¦ããã®ã§ãããDIãDIã³ã³ãããä½ãããã£ã¦ããªãã®ã§åå¼·ãã¦ã¿ããã¨æãå°ã試ãã¦ã¿ãã®ã§ã¡ã¢ã
ã¾ãã¯DIç·¨ã
DIã¨ã¯
Dependency Injectionã§ãã
ä¾åæ§ã®æ³¨å ¥ã§ãã
...ï¼(´ã»Ïã»`)
ä¾åæ§ï¼(´ã»Ïã»)注å
¥ï¼(´ã»Ïã»
)ã£ã¦ããæããªã®ã§ããå°ã詳ããæ·±æã£ã¦ã¿ã¾ãã
ä¾åæ§ã¨ã¯
ãã¯ã©ã¹Aãæ£ããåããããã«ã¯ãã¯ã©ã¹Bãæ¢ã«åºæ¥ä¸ãã£ã¦ãªãã¨ã ããã¨ããç¶æ ãããã¯ã©ã¹Aã¯ã¯ã©ã¹Bã«ä¾åãã¦ãããã¨ããã¾ãã
ä¾ã示ãã¾ãã(ããããè¨ããã¨ã¯ããã©ã¨ããããä¾åãã¦ããäºããããå®è£ ã«ãªã£ã¦ããã°ããã¨ãã¾ã)
<?php class Song { public function __construct($title=null) { $this->title = $title; } public function getTitle() { return $this->title; } } class MusicPlayer { private $song; public function __construct() { $this->song = new Song('test'); } public function play() { echo '"'.$this->song->getTitle().'"ãåçããã'; } } class TwitterClient { public function post() { // æ稿å¦ç echo 'é³ã²ã¼ããã¬ã¤ãããã¨ã¤ã¶ããã'; } } class Otoge { private $song; private $music_player; private $twitter_client; public function __construct() { $this->song = new Song('test'); $this->music_player = new MusicPlayer(); $this->twitter_client = new TwitterClient(); } public function play() { echo '"'.$this->song->getTitle().'"ã§é³ã²ã¼ãéå§ããã'; $this->music_player->play(); // ãã®ä»å¾ç¹è¨é²ãããã¨ãã®å¦ç } public function tweet() { $this->twitter_client->post(); } }
å®è¡ä¾ã¯ãããªæãã§ãã
<?php $otoge = new Otoge(); $otoge->play(); $otoge->tweet();
ãã®ä¾ã§ã¯ã
- é³ã²ã¼å ã§æ²æ å ±ãæ±ããã
- é³ã²ã¼å ã§é³æ¥½ãåçããã
- é³ã²ã¼å ãããã¤ã¼ããæ稿ããã
- é³æ¥½ãã¬ã¤ã¤ã¼å ã§ãæ²æ å ±ãæ±ããã
ã¨ããè¦ä»¶ãããã¨ãã¾ãã
ãã®ä¾ã ã¨ã次ã®ãããªä¾åé¢ä¿ãããã¨è¨ãã¾ãã
- ã¯ã©ã¹Otogeã¯ã¯ã©ã¹Songã«ä¾åãã¦ãã
- ã¯ã©ã¹Otogeã¯ã¯ã©ã¹MusicPlayerã«ä¾åãã¦ãã
- ã¯ã©ã¹Otogeã¯ã¯ã©ã¹TwitterClientã«ä¾åãã¦ãã
- ã¯ã©ã¹MusicPlayerã¯ã¯ã©ã¹Songã«ä¾åãã¦ãã
ä¾åèªä½ã¯æªããã¨ã§ã¯ããã¾ãããè¦ä»¶ããã£ã¦å½¹å²ã«å¿ãã¦ã¯ã©ã¹ãåãã¦ãã£ããèªç¶ã¨è¦ä»¶ä¸ä¾åé¢ä¿ã«ããã¯ã©ã¹ãã¡ã¯çã¾ãã¦ããã§ãããã
ãããä½ããããªãã®ã
ä¾åé¢ä¿ãããã¿æ¸ããããã¦ãããã¨ã§ãã
ã¯ã©ã¹Otogeã®ã³ã³ã¹ãã©ã¯ã¿ã«æ³¨ç®ãã¦æ¬²ããã®ã§ãããã³ã³ã¹ãã©ã¯ã¿å ã§Song, MusicPlayer, TwitterClientã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã¦ããããã£ã«ã»ããããã¦ãã¾ããã
ã¾ããã¯ã©ã¹MusicPlayerã®ã³ã³ã¹ãã©ã¯ã¿ã§ããSongã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã¦ããããã£ã«ã»ããããã¦ãã¾ãã
ããã¯ä¾åãã¦ããã¯ã©ã¹ã«ä¾åé¢ä¿ãããã¿æ¸ããããã¦ããã¨è¨ããã§ãããã(大äºãªãã¨ãªã®ã§2å(ry)
ãã®ç¶æ ã ã¨ã以ä¸ã®ãããªãããããªãç¹ãèãããã¾ãã
- Song, MusicPlayer, TwitterClientãå¥ã®ãã®ã«æ¿ãããã¨ãã«Otogeã«ä¿®æ£ãå ããªããã°ãªããªããªã
- Song, MusicPlayer, TwitterClientãä¸æãåããªãã¨Otogeã«å¯¾ãã¦ã®ãã¹ããã§ããªããªã
- Song, MusicPlayer, TwitterClientãåºæ¥ä¸ããã¾ã§Otogeã®éçºãã§ããªããªã
- Songãå¥ã®ãã®ã«æ¿ãããã¨ãã«MusicPlayerã«ä¿®æ£ãå ããªããã°ãªããªããªã
- Songãä¸æãåããªãã¨MusicPlayerã«å¯¾ãã¦ã®ãã¹ããã§ããªããªã
- Songãåºæ¥ä¸ããã¾ã§MusicPlayerã®éçºãã§ããªããªã
- Otoge->tweet()ããã¹ããããã³ãã¤ãã¿ã¼ã«æ稿ããã¦ãã¾ã
- MusicPlayer->play()ããã¹ããããã³é³æ¥½ãåçããã¦ãã¾ã
ãããããããããªãç¹ãåºã¦ãã¦ãã¾ãã®ã¯ã¾ããã®ã§ããªãã¨ããããã§ããã
ãããã©ããããããã®ã
諸æªã®æ ¹æºã¯ä¾åé¢ä¿ãããã¿æ¸ããããã¦ãããã¨ã§ãã(大äºãªãã¨ãªã®ã§3å(ry)
ãªã®ã§ããã¿æ¸ããããªãããã«ãã¾ãããã
ããã¿æ¸ããããªã = ã¯ã©ã¹å ã§ã¤ã³ã¹ã¿ã³ã¹ãçæããªã
ã¤ã¾ã
ããã¿æ¸ããããªã = ã¯ã©ã¹å¤ã§ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦å¤ãã渡ãã¦ããã
ã¨ããã¾ãã
ãããDI(ä¾åæ§ã®æ³¨å ¥)ã¨è¨ãããã§ãï¼ï¼
DIããã
å ·ä½çã«ã¯ãDIã¨ã¯ã©ã®ãããªå½¢ã§è¡ããã¦ããã®ã§ãããããã©ããããå ã»ã©æãããããããªãç¹ã解æ¶ã§ããã§ãããããä¾ã示ãã¾ãã
<?php interface SongInterface { public function getTitle(); // ...ãªã©æ²ã«é¢ãã諸ã } class Song implements SongInterface { public function __construct($title=null) { $this->title = $title; } public function getTitle() { return $this->title; } // ... } interface MusicPlayerInterface { public function play(); } class MusicPlayer implements MusicPlayerInterface { /** * @var SongInterface */ private $song; public function __construct(SongInterface $song) { $this->song = $song; } public function play() { echo '"'.$this->song->getTitle().'"ãåçããã'; } } interface TwitterClientInterface { public function post(); } class TwitterClient implements TwitterClientInterface { public function post() { // æ稿å¦ç echo 'é³ã²ã¼ããã¬ã¤ãããã¨ã¤ã¶ããã'; } } class Otoge { /** * @var SongInterface */ private $song; /** * @var MusicPlayerInterface */ private $music_player; /** * @var TwitterClientInterface */ private $twitter_client; public function __construct(SongInterface $song, MusicPlayerInterface $music_player, TwitterClientInterface $twitter_client) { $this->song = $song; $this->music_player = $music_player; $this->twitter_client = $twitter_client; } public function play() { echo '"'.$this->song->getTitle().'"ã§é³ã²ã¼ãéå§ããã'; $this->music_player->play(); } public function tweet() { $this->twitter_client->post(); } }
å®è¡ä¾ã¯ããããªæãã§ãã
<?php $song = new Song('test'); $music_player = new MusicPlayer($song); $twitter_client = new TwitterClient(); $otoge = new Otoge($song, $music_player, $twitter_client); $otoge->play(); $otoge->tweet();
DIãã¦ããªãç¶æ ããå¤ãã£ããã¨ã¯ã以ä¸ã®ãããªç¹ã§ãã
- ã³ã³ã¹ãã©ã¯ã¿å ã§ã®ã¤ã³ã¹ã¿ã³ã¹çæãè¡ããªããªã£ã
- å¼æ°ã¨ãã¦ã¤ã³ã¹ã¿ã³ã¹ãåãåãããã«ãªã£ã
- ã¤ã³ã¹ã¿ã³ã¹ã¯ããããInterfaceãimplementsãããã®
ããããã¨ãOtogeãMusicPlayerã®ã¤ã³ã¹ã¿ã³ã¹ãçæããéã«å¼æ°ã§å¿ è¦ãªãã®ãæ³¨å ¥ãã¦ãããã°ããã®ã§ãä¾åæ§ãããã¿æ¸ãããããã¨ã¯åé¿ã§ãã¾ããï¼
ãªããInterfaceã¯å¿ ãããå®è£ ããªãããã¨ããããã§ã¯ããã¾ããã
å®è£ ãç¥ã£ã¦ãã¾ã£ã¦ãã¦ãè¯ãå ´åã¯ã¯ã©ã¹ã§ãã£ã¦ãã¾ã£ã¦ãå¤é¨ã«å ¬éããå¿ è¦ããããããªé¨åã¯interfaceã§ãã£ã¦ãã¾ããã¨ããã®ãããããã¾ãã
ä»åã¯DIãããããããããããã«ãã¹ã¦interfaceãå®è£ ãã¦ãã¾ãããé©å®ä½¿ç¨ããããã«ããã¨ä¿®æ£ã®æéãå°ãªããªã£ã¦ããå¹çããéçºãè¡ãããã¨æãã¾ãã
DIãã¦ãããã¨ã®å¯ä½ç¨
ä¾åæ§ãæ³¨å ¥ã§ããããã«ãªã£ãããã§ãããæ°ã«ãªãç¹ãã²ã¨ã¤ã
ãå¼æ°ççºãã¾ããï¼ã
ããã§ããããä¾åæ§ãçã¾ãããã³ã«å¼æ°ãã²ã¨ã¤å¢ãã¦...ã¨ãªã£ããå¼æ°ãå¤ããªã£ã¦ç®¡çããã®ãé¢åã«ãªã£ã¦ãããã§ãã
å¼ã³åºãå ã§ã®å®è£ ããã¡ãã¡åã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ãã£ã¦é¢åã§ããã俺ã¯Otogeã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãããã£ãã ããªã®ã«...ãã£ã¦ãªãããã§ããã
ããã§DIãã³ã³ãããã®ç»å ´
ãããªæ©ã¿ããDIã³ã³ãããã¨ãããã®ã使ãã¨ãã£ãããã¦ç®¡çã楽ã«ãªãã¾ãã
å¾ç·¨ã§ã¯PHPã®DIã³ã³ããã®ä¸ã¤ã§ããPimpleãç¨ãã¦DIã³ã³ããã«ã¤ãã¦æ¸ãã¦ããããã¨æãã¾ãã
å¾ç·¨ã¯ãã¡ã