è¤éãã«æ½ãè¾¼ã - 大è¦æ¨¡PHPã¢ããªã±ã¼ã·ã§ã³ã«ãããä¾å¤ã»ã¢ãã¿ãªã³ã°ã»ãã®ã³ã°
ã¿ãªãããPHPæ¸ãã¦ã¾ããï¼ãã2ã¶æãããPHPãæ¸ãã¦ããã®ã§ãã®è©±ãæ¸ãã¾ãã
ãã®è¨äºã¯VOYAGE GROUP techlog / Advent Calendar 2016ã®è¨äºã§ãã
ä¾ãã°ä»¥ä¸ã®ãããªè©±ã«èº«ã«è¦ãã¯ããã¾ããã§ããããã
- ä¾å¤ãã©ããã§æ¡ãã¤ã¶ããã¦ãããä¾å¤çç¶æ³ãªã®ã«ã¨ã©ã¼è¡¨ç¤ºãã¾ã¡ã¾ã¡ãã¬ã¹ãã³ã¹ã¹ãã¼ã¿ã¹ãä¸è²«æ§ããªããã¨ã©ã¼ãã°ãé©åã«åºã¦ããªãã
- ã¨ã©ã¼ãã°åºåç¨ã³ã¼ããããããªã¨ããã§æ£ä¹±ãã¦ãããã¨ã©ã¼æååæ´å½¢ã®ããã®é©å½ãªãã«ãã¡ã½ãããã¯ã©ã¹ãã¨ã«å®è£ ããã¦ããã
- ã¨ã©ã¼ãã¨ã«ã¨ã©ã¼è¡¨ç¤ºã®ããã®ã¡ãã»ã¼ã¸ãè¨å®ããã®ãé¢åã«ãªãããã·ã¹ãã ã¨ã©ã¼ãèµ·ãã¾ãããã¨ã ã表示ãããããã«ãªã£ã¦ãã¾ã£ãã
- ä¾å¤ãã³ããªã³ã°å¨ãã®ã³ã¼ãã¯èããã®ãé¢åãªã®ã§ã³ããã ããã«ãªã£ã¦ããã
- ãªãã¸ã§ã¯ãã®ä¾åé¢ä¿ãã¯ã©ã¹ã®ããããã£ã«å¤§éã«åãè¾¼ã¾ãã¦ããããããåæåããããã®ãã¸ãã¯ãã³ã³ã¹ãã©ã¯ã¿ãªããåæåã®ããã®ã¡ã½ããã«çµã¿è¾¼ã¾ãã¦ãããããã親ã¯ã©ã¹ã¨ãªããç¶æ¿ãããã¯ã©ã¹ã«ã¯æé»çã«å¤§éã®ããããã£ãç¶æ¿ããã¦ãããã¾ããã®ãããã¹ãã³ã¼ããã大éã®åæåå¦çãå¿ è¦ã«ãªãã
ãããªåé¡ã解決ãã¹ããããæè¿åãçµãã§ãã話ããã¾ãã
ç°å¢
- PHP 5.3.x / CodeIgniter 1.7.x
- 5,6å¹´ã»ã©éç¨ããã¦ããPHPã¢ããªã±ã¼ã·ã§ã³ / ã ããã10ä¸è¡ããã
- 社å / 社å¤ã®ã¦ã¼ã¶ãå©ç¨ãã管çç¨ã®ã¢ããªã±ã¼ã·ã§ã³
- php-cs-fixerã§ã»ã¨ãã©ã®ã³ã¼ãã®ä½è£ã¯æ´ã£ã¦ãã
- Class Loaderã¯CodeIgniterã¨composerã®ãã®ãä½µç¨ããã¦ãã / ãã¾ã«
require_once
ããã
å ã«ãè§ã«ãã¢ãã¿ãªã³ã°
ã¢ããªã±ã¼ã·ã§ã³ã®èª²é¡ã¯ä½ãï¼ããã¼ã ã§èãå§ããã¨ãã以ä¸ã®ç¹ã話ãåããã¾ããã
- ä½ãã©ãã§å¤±æãã¦ããããããã¥ãã
- æ¢åã³ã¼ããèªã¿è§£ãã³ã¹ããé«ã / ä¸å¿ è¦ã«è¤éã«ãªã£ã¦ãã
- ãã使ããã¦ããã³ã¼ãã«è²»ããæéãå°ãªããæ°ããæ©è½ã«æéãå²ãããã¡ã«ãªã
- å½æã¡ã³ããã³ã¹ãã¦ããã¨ã³ã¸ãã¢ãããªããªã£ã¦ãããããã«æ§é ã«ã¤ãã¦èãã人ãããªã
ã©ãã大äºã§ãããå人çã«ã¯ã¢ããªã±ã¼ã·ã§ã³ã¨ã³ã¸ãã¢ã®è¡åæéã¨ãã¦ç¹ã«3çªç®ã¯å¤§äºã ã¨æã£ã¦ãã¾ããæ°æ©è½ã¯ãã¡ãããã¸ãã¹ã®ç®çã«ãªãããããã¾ããããæ¦ç¥ä¸éè¦ãªå ´åãå¤ãã§ããããããããªãããæ¢åã³ã¼ãã¯ä¾¡å¤ãçã¿åºãç¶ãã¦ãããäºæ¥ãæ¯ãã¦ããã¨ããå ´é¢ã¯å°ãªãããã¾ãããè²»ããæéãå°ãªããªãã°ã³ã¼ãã®ä¿å®ã¯çãã«ãªãã¾ããã¨ã³ã¸ãã¢ãã¼ã ã¨ãã¦ä¿å®ãã§ã¼ãºã«å ¥ã£ã¦ããæ©è½ãã©ã®ããã«è³ªãä¿ã¡ã¤ã¤ã¡ã³ããã³ã¹ãã¦ãããã¨ããã®ã¯èª²é¡ã§ãã
ããã§ã¾ãããã使ããã¦ããã³ã¼ãã§åé¡ãèµ·ãã£ã¦ããç®æãå¹çããç¥ãæ¹æ³ã¯ãªãã ãããã¨èãã¾ãããçµè«ã¨ãã¦ã¯New Relicã§ã¢ããªã±ã¼ã·ã§ã³ã¢ãã¿ãªã³ã°ãã¯ããã¾ããã
Application Performance Management & Monitoring | New Relic
New Relicã§ã¯ä»¥ä¸ã®ãã¨ãã§ãã¾ãã
- æç³»åã§ã®PHPã©ã³ã¿ã¤ã ç¶æ³ã®ææ¡
- å®è¡ã«æéã®ããã£ã¦ããã³ã¼ãã®ãããã¡ã¤ãªã³ã°
- é ãã¯ã¨ãªã®æ½åº / 1ã¤ã®HTTP requestãã¨ã«çºçããã¯ã¨ãªã®æ¬æ°ã¨æè¦æéãªã©ã®ææ¡
- ã¨ã©ã¼ç¶æ³ã®ã¢ãã¿ãªã³ã° / HTTP Requestãã¨ã«çºçããã¨ã©ã¼å 容ã®ææ¡
ãã¾çæãã¦ããã¢ããªã±ã¼ã·ã§ã³ã¯CodeIgniterã®1.7ç³»ããã¼ã¹ã¨ãã¤ã¤ä¸é¨ã®APIã§ã¯Slim Frameworkãå©ç¨ããã¦ãã¾ãããã®ãããç¹å®ã®ãã¬ã¼ã ã¯ã¼ã¯ã«ãããããã¡ã¤ãªã³ã°ãã¼ã«ã§ã¯ãªããPHPã©ã³ã¿ã¤ã ã®æ å ±ãããã¬ã¼ã¹ãã¦ããããããªãã¼ã«ãæ¢ãã¦ãã¾ãããNew Relicã®å ´åã¯ä»åã®ã¦ã¼ã¹ã±ã¼ã¹ã«ãã£ããããå©ç¨ãéå§ãã¾ãããDBå´ã§ãã¹ãã¼ã¯ã¨ãªãã°ãåºåãã¦ãã¾ãããNew Relicããã ã¨ã©ã®HTTP Requestã®ãã¿ã¼ã³ã«ããã¦ä½æ¬ã¯ã¨ãªãçºè¡ããã¦ããã¬ã¹ãã³ã¹ã¿ã¤ã ãé·ãããè¦å ã«ãªã£ã¦ããããã¨ãããã¨ãææ¡ãããããªã£ã¦ãã¾ãããã®ãããã¯OR Mapperãæ¹è¯ãã¦ä¾ãã°SQLã³ã¡ã³ãã«HTTP requestãã¨ã®IDãåãè¾¼ã¿ãtraceabilityã確ä¿ããæ¹æ³ãããã¾ããç§éã®å®è£ ã§ã¯OR Mapperãå¿ ã使ããã¦ããããã§ã¯ãªãã£ãã®ã§ãã¢ãã¿ãªã³ã°ãã¼ã«å´ã§ãã¬ã¼ã¹ã§ãããã¨ã¯å½¹ç«ã¡ã¾ããã
ããã使ãããã³ã¼ãã¨ã¯ã©ãããã¨ãã観ç¹ã¯ãã¡ããã®ãã¨ãã¦ã¼ã¶ã«ã¨ã£ã¦ã¹ãã¬ã¹ã®ããã£ã¦ããç»é¢ / URLã¯ã©ãããã¨ããã®ãã¢ããªã±ã¼ã·ã§ã³ããã©ã¼ãã³ã¹ã¢ãã¿ãªã³ã°ã«ãã£ã¦éçºè éã§å ±æã§ããããã«ãªã£ãã®ã¯å¤§ããªå¤åã§ãããããã«ãããããã©ã¼ãã³ã¹ã«åé¡ã®ããç®æã®issueãæ確ã«ã§ããã®ã¯ããã¾ã§ãããã¾ããã
catch (Exception $e)
ã®é¦ã / ã¢ããªã±ã¼ã·ã§ã³ãã¼ã¹ä¾å¤ã®è¨è¨ã¨ãã³ããªã³ã°
ããã§è³ªåã§ããMVCã«ãããã³ã³ããã¼ã©ã¯ã©ã¹ã®ããã¡ã½ããã§ä»¥ä¸ã®ã³ã¼ããããã¾ããããã®ã³ã¼ãã®å¥½ã¾ãããªãç¹ã¯ã©ãã§ããããã
<?php try{ // DBã«ã¤ãªãã§çµæè¿ããã // 権éãã§ãã¯ããã // ã¨ã«ããè²ã ãªå¦ç } catch (Exception $e) { error_log($e->getMessage()); $this->tpl->display('error.html', array('message' => $e->getMessage())); }
è²ã ããããã§ãããååã§ããããªãã®ã§ãããããªã³ã¼ãã300ç®æå¼±ããã¾ãããç§ãæåãããè¦ãã¨ãã«æã£ãã®ã¯ä»¥ä¸ã®ç¹ã§ãã
- ã«ã¼ãä¾å¤ã§
catch
ãã¦ããã®ã¯ãªãï¼ - ã¹ã¿ãã¯ãã¬ã¼ã¹ã¯ã©ãã«ãåºåãã¦ããªããã©ããã§
catch
ããã¾ãã«ã©ããã§åºãã¦ããã®ã ãããã»ã»ï¼ $this->tpl
ãããããæ¡ã£ã¦ãããªæ°é ããã- ã¨ã©ã¼ç»é¢ã«ä¾å¤ã®ã¡ãã»ã¼ã¸ãã®ã¾ã¾åºãã¦ããã©ããã°ã¨ç»é¢è¡¨ç¤ºã«åãã¡ãã»ã¼ã¸åºãã¦å¾ããåå 追ããã®ã ãããï¼
- ï¼ã±ã£ã¨è¦ããããªããã©ï¼ããã¬ã¹ãã³ã¹ã¹ãã¼ã¿ã¹è¿ãã¦ããªãã®ã§ã¯ï¼
catch
å¥ã®ä¸èº«ãã ãããã³ãã
ä»ã«ãç´°ããç¹ã¯è²ã ããã¾ãããã±ã£ã¨æã£ãã®ã¯ããããã¨ããã§ããçµæçã«ãããæ±ãã ã¨ãã³ã¼ãã¹ã¿ã¤ã«ã ã¨ããç½®ãã¦ããã¦ä»¥ä¸ã®ç¹ãããããã¾ããã§ããã
- ã¹ã¿ãã¯ãã¬ã¼ã¹ããªãã®ã§åå 究æããã¥ãããªã£ã¦ãããè«çä¾å¤ãèµ·ãã¦ã対å¿ãæ¾ç½®ããã¦ãããéçºç°å¢ã§ã®ã¿ã¹ã¿ãã¯ãã¬ã¼ã¹ã表示ããã¨ããå¶å¾¡ãã§ããªãã
- ã¢ããªã±ã¼ã·ã§ã³ä¾å¤ã
throw Exception("å¦çã«å¤±æãã¾ãã")
ã®ããã«ã«ã¼ãä¾å¤ã«ã¡ãã»ã¼ã¸ã ãè¨å®ãã¦throw
ããã¦ããç®æãå¤ã ããã - ã¨ã©ã¼ãã°ã®å 容ãå°ãªãããããã¤ã©ãã§ä½ãè¦å ã§å¤±æãã¦ããã®ãããã¨ãããã¬ã¼ã¹ã§ããªã
- ã¨ã©ã¼è¡¨ç¤ºã®ããã®
tpl
ãã¯ã©ã¹ã®ããããã£ï¼å®éã«ã¯è¦ªã³ã³ããã¼ã©ã®ããããã£ï¼ã«ä¾åãã¦ããã®ã§ã³ã¼ãã®åé¢ããã¥ãããªã£ã¦ããããã®ããã¨ã©ã¼è¡¨ç¤ºã®ããã®ã³ã¼ããã³ã³ããã¼ã©ã«å ¥ããããå¾ãªããªã£ã¦ããã - ã¨ã©ã¼ãã°åºåã®ããã®ã³ã¼ããå
catch
ã®ä¸ã§æ¸ããã¦ããããããã¾ã«åºåãå¿ãããã¦ãããã¨ã©ã¼ãèµ·ãã¦ãããã¨ããææ¡ã§ããªãç¶æ³ã«ãªã£ã¦ããé¨åããã£ãã
ãããã®èª²é¡ã«ã¢ããã¼ãããããã«ã¯ã¢ããªã±ã¼ã·ã§ã³æ§é ããçæããã®ãå¹çãããã¨èãã¾ãããããã§ãä»å®æçã«ã³ã³ãµã«ã«æ¥ã¦ããã ãã¦ãã @t_wada ããã¨ãç¸è«ãã¤ã¤ã以ä¸ã®æ¹éãç«ã¦ã¾ããã*1
- 大åã®ä¾å¤ãã³ãã©ãå®è£
ãããä¾å¤ã
throw
ããã°é©åã«ã¨ã©ã¼ç»é¢ã表示ãããããã«ãªããã¨ãã¾ãä¾å¤ã«å¿ãã¦é©åã«HTTP response statusãè¨å®ããããã¨ããããããã¦ãã¦ã¼ã¶ã®åå®è£ ã«ããã¦try
catch
ã¯å¿ è¦æä½éã«ãããã¨ã - ç¬èªå®ç¾©ã®ä¾å¤ã¯ãã¹ã¦ãã¼ã¹ä¾å¤ãç¶æ¿ãããã¨ããã¼ã¹ä¾å¤ã«ã¯ç»é¢è¡¨ç¤ºåãã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¨ã·ã¹ãã åãã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ãåãã¦ä¿æã§ãããã¨ã
- ä¾å¤ã
catch
ãã¦æåãå¤æ´ããå ´åã«ããç¹å¥ãªå ´å*2以å¤ã«ã¯ä¾å¤ãæãç´ããã¨ãä¾å¤ãæãç´ãå ´åã«ã¯ã¹ã¿ãã¯ãã¬ã¼ã¹ã第ä¸å¼æ°ã§å¼ãç¶ããã¨ã
ãã¼ã¹ä¾å¤ã¯ä»¥ä¸ã®ãããªè¨è¨ã«ãªã£ã¦ãã¾ãã
<?php // 注: Throwableã¯ç¬èªå®è£ ã«ãªã£ã¦ãã¦ãPHP 7ã®ããã®polyfillã§ãã class BaseException extends \Exception implements Throwable { protected $userMessage; /** * @param string $message * @param int $code * @param \Exception|NULL $previous * @param string $userMessage */ public function __construct($message = "", $code = 0, \Exception $previous = null, $userMessage = "") { parent::__construct($message, $code, $previous); $this->userMessage = $userMessage; } }
$userMessage
ï¼ãã®ãã¼ãã³ã°ããã£ã¨ãããã®ããã£ãã ããã¨ã¯æãã¾ããã²ã¨ã¾ãã¯ããã§ã»ã»ï¼ã«ã¯ã¦ã¼ã¶ã«è¦ããããã®æååãè¨å®ãã¾ããã³ã³ã¹ãã©ã¯ã¿ã¯ PHP: Exception - Manual ã®ã³ã³ã¹ãã©ã¯ã¿ã«ç¬¬4å¼æ°ã追å ãã¦ãããããã«ã¦ã¼ã¶è¡¨ç¤ºç¨æååãè¨å®ã§ããããã«ãã¦ãã¾ããç¬èªä¾å¤ãå®ç¾©ããå ´åã«ã¯ãã®ã¦ã¼ã¶åãã¡ãã»ã¼ã¸ãåä¾å¤ã®ããã©ã«ãå¤ã¨ãã¦è¨å®ã§ãã¾ããããããã¯ä¾å¤ã throw
ããéã«é©å®çæãã¦åãè¾¼ããã¨ãã§ããã¨ããè¨è¨ã«ãªã£ã¦ãã¾ããã¾ããã¨ã§ãåºã¦ãã¾ããããã¼ã¹ä¾å¤ã® $code
ã«ã¤ãã¦ã¯HTTP response status codeã¨ãã¦å©ç¨ãããããã«ãªã£ã¦ãã¾ããããã¯ãWebã¢ããªã±ã¼ã·ã§ã³ã®ä¾å¤ã³ã¼ãã¯HTTP response status codeã¨ããã®ãæ確ãªã®ã§ã¯ãªãããã¨ããèãããã§ãããã®ããã㯠@t_wada ããã¨è°è«ãã¦ãã¦æ°ãã¤ãããã¨ã§ãããPHPã®ä¾å¤ã³ã¼ãã¯æ¡å¤ä½¿ããã¦ããªãã¨ãã話ã«ãªãããããªãHTTP response status codeã¨ãã¦ãã¾ãã®ãè¦éããããããã§ãããã¨ãã話ããã¾ããã
PHPã§ã¯å¤§åã®ä¾å¤ãã³ãã©ãè¨å®ããããã« PHP: set_exception_handler - Manual ãå©ç¨ã§ãã¾ãã set_exception_handler
ãå©ç¨ããã¨ããã¹ã¦ã® throw
ããã¦ããä¾å¤ã«ã¤ãã¦ã®ãã³ãã©é¢æ°ãè¨å®ã§ãã¾ãã*3ããã¯ãã¬ã¼ã ã¯ã¼ã¯å´ã§ã®ä»äºã§ãªããã°ãªãã¾ãããç§éããã®ã¢ããªã±ã¼ã·ã§ã³ã§å©ç¨ãã¦ããCodeIgniter 1.7ç³»ã§ã¯å¤§åä¾å¤ãã³ãã©ã¯ããã¾ãããããã§ãã¬ã¼ã ã¯ã¼ã¯åæåæã« set_exception_handler
ã«ãã£ã¦å¤§åä¾å¤ãã³ãã©ãè¨å®ããããã«å¤æ´ãã¾ããã*4
ããã§ãã§ãã大åä¾å¤ãã³ãã©ãä»äºããã¦ãããã»ã»ã¨æã£ãããã§ãããã¾ã 工夫ãããå¿ è¦ãããã¾ããã大åä¾å¤ãã³ãã©ãããã¨ã©ã¼è¡¨ç¤ºç¨ã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãå©ç¨ããå¿ è¦ãããã¾ããããã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®åæåå¦çã¯ã³ã³ããã¼ã©ã®è¦ªã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã§åæåããã¦ãããã³ã³ããã¼ã©ããã¯å©ç¨ã§ããªããã¨ããç¶æ³ã«ãªã£ã¦ãã¾ããã*5ããã§DIã³ã³ãããå°å ¥ãã¾ããã
DIã³ã³ããã®å°å ¥
大åä¾å¤ãã³ãã©ã§ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãå©ç¨ããããã«ã¾ã åé¡ãããã¾ããã親ã³ã³ããã¼ã©ã®ã³ã³ã¹ãã©ã¯ã¿ã«å¤§ããä¾åãã¦ããã¢ããªã±ã¼ã·ã§ã³åæåå¦çãããã¾ããã
Webã¢ããªã±ã¼ã·ã§ã³ã®å ¥ãå£ã¯ã©ããï¼ã¨ããã¨HTTP Requestã§ãããªã®ã§ãã¬ã¼ã ã¯ã¼ã¯ã®ã¦ã¼ã¶ããããã¨ã³ã³ããã¼ã©ã®åæåæãããªãã¡ã³ã³ã¹ãã©ã¯ã¿ã«è²ã æ¸ããããªã£ã¦ãã¾ãã®ã§ããããSlimãSilexãªã©ã®ãã¤ã¯ããã¬ã¼ã ã¯ã¼ã¯ã ã¨ã³ã³ããã¼ã©ã§ã¯ãªãRouterãªã®ã§åæåå¦çã¯DIã³ã³ããã§ããã¨ããã®ã¯èªç¶ããããã¾ãããã¨ã¯ããDIã³ã³ããã®ã¤ã³ã¿ãã§ã¼ã¹ãç¨æããã¦ããªãå ´åãã³ã³ããã¼ã©åæåæã«ã¢ããªã±ã¼ã·ã§ã³ã§å©ç¨ããæ§ã ãªã³ã³ãã¼ãã³ãã®åæåå¦çãæ¸ãã¨ããé¸æã¯ãããã¡ã ã¨æãã¾ããä¾ãã°ã»ãã·ã§ã³ãèªã¿åãã¤ã¤æ¨©éããã§ãã¯ããã³ãã¬ã¼ãã¨ã³ã¸ã³ã®åæåãã¤ã¤ä¾¿å©ã¡ã½ãããçããããããã¼ã»ããã¿ã¼ãªã©å ±éé¨åã®ã³ã³ãã¼ãã³ãããã°ã¤ã³æ å ±ããã¨ã«åæåãããDBã«æ¥ç¶ãã³ãã¯ã·ã§ã³ãä¿æããããªã©ã§ããããã¯Webã¢ããªã±ã¼ã·ã§ã³ã®testabilityããããã¯ã³ã³ããã¼ã©ã®testabilityãä¸ããè¦å ã§ãããã¾ããã³ã³ããã¼ã©ãç¶æ ãæã¡ããã¦ããããã§ãã
ããã§ç§éã¯æ®µéçã«ãã®ä¾åé¢ä¿ã®ç®±ã移ãã¦ãããã¨èãã¾ãããä»ã®è¦ªã³ã³ããã¼ã©ã«åæåå¦çãä¾åããç¶æ ã§ã¯ã³ã³ããã¼ã©ä»¥å¤ããã»ãã·ã§ã³ãã¤ãã£ãã³ã¼ããæ¸ãããããã³ãã¬ã¼ãã¨ã³ã¸ã³ãå©ç¨ããã³ã¼ããæ¸ããã¨ãã§ãã¾ãããããã¯ã¢ããªã±ã¼ã·ã§ã³ã®èªç±åº¦ãå¢ããã¾ãããããæå³ã§è¤é度ãå¢ãé¸æã§ãããã®é¸æã«ã¯æ éã§ããããDIã³ã³ããã¯è»½éãã¤disposableãªã³ã³ãã¼ãã³ãã¨ãã¦å©ç¨ã§ããã¨èããé²ããå¤æããã¾ããã*6
DIã³ã³ãããä½ãã親ã³ã³ããã¼ã©ã®ã³ã³ã¹ãã©ã¯ã¿ã«ãããåæåãã¸ãã¯ã移æ¤ãã¾ãããããã¯ã¢ããªã±ã¼ã·ã§ã³ã®ã©ãããã§ãå©ç¨ã§ããããã«ãªã£ã¦ãã¾ããå
·ä½çã«ã¯CodeIgniterã¯ã«ã¼ããªãã¸ã§ã¯ã*7 ã &= get_instance()
ã«ãã£ã¦ã¢ããªã±ã¼ã·ã§ã³ã®ã©ãããã§ãåå¾ã§ãã¾ãããã®ä»çµã¿ã«ä¹ã£ãããDIã³ã³ãããã«ã¼ããªãã¸ã§ã¯ãçµç±ã§åå¾ã§ããããã«ãã¾ããããã®ãããã¯ãã¼ã ã¡ã³ãã¼ãããã°ã£ã¦ããã¾ããã
<?php $ci =& get_instance(); $container = $ci->pimple->container;
DIã³ã³ãããçµç±ãããã¨ã§ã大åä¾å¤ãã³ãã©ã§å©ç¨å¯è½ãªåææ¸ã¿ã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³åã³ã»ãã·ã§ã³æ å ±ãåå¾ã§ããããã«ãªããå¾æ¥éãã®ã¨ã©ã¼ãã¼ã¸ã表示ã§ããããã«ãªãã¾ãããã¡ãªã¿ã«DIã³ã³ããã«ã¯Pimpleã使ã£ã¦ãã¾ãã
大åä¾å¤ãã³ãã©ã®å®è£
ã¯æ¦ã以ä¸ã®ããã«ãªãã¾ããããã¬ã¼ã ã¯ã¼ã¯åæåæã« post_controller_constructor
Hookãããããå¼ã¶ãã¨ã§å¤§åä¾å¤ãã³ãã©ãè¨å®ãã¦ãã¾ãã
<?php class ExceptionHook { public function setExceptionHandler() { set_exception_handler(array($this, "handler")); } /** * @param Exception $e */ public function handler(Exception $e) { $ci =& get_instance(); $container = $ci->pimple->container; $twig = $container['twig']; log_message("é©å½ãªã¨ã©ã¼ã¡ãã»ã¼ã¸ãããæãã«ããã«åãè¾¼ã¾ããç§å¯ã®ä»çµã¿", $e->getMessage()); if ($e instanceof BaseException) { // ä¾å¤ã³ã¼ããHTTPã¬ã¹ãã³ã¹ã¹ãã¼ã¿ã¹ã³ã¼ãã¨ãã¦åãã header('HTTP', true, $e->getCode()); $twig->display('error.html', $e->getUserMessage); return; } header('HTTP', true, 500); $twig->display('error.html', $e->getMessage()); } }
ããã§ããããä¾å¤ã®è¶³åããæ´ãã¾ããã throw
ããã°ããæãã«HTTP responseãçæãããã¨ã©ã¼ãã°ãåºåãããä¸çã«ãªãã¾ããã
ã¨ã¯ãããä¾å¤ã«é¢ããã®ã³ã¼ãã¯ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãå ¨ä½ããã¿ãã¨ããä¸é¨ã§ããã¡ã³ããã³ã¹æ§ãé«ããã«ã¯ãããã¨ãã¾ã ã¾ã ããã¾ãã
使ããã¦ããªãã³ã¼ããæ¶ããã¨ã¯ã¡ã³ããã³ã¹æ§ãé«ãã
ç¥ããªãã³ã¼ããèªãã¨ããã©ã®ããã«èªã¿ã¾ããï¼ç§ã¯ç¥ããªãã³ã¼ããèªãã¨ããé¢æ°ã§ããã°ã¾ãinputã¨outputãæèãã¦èªã¿è§£ãã¾ããå¯ä½ç¨ããªããªãããã¯ããã£ã¨çµããã¾ãã»ã»ããããé¢æ°å¤é¨ã®ç¶æ ãå¤æ´ããã¦ããã°é ã®ä¸ã®ã¹ã¿ãã¯ã溢ãããã«ãªã£ã¦ãã¾ãããã«ãªããªãããç¶æ ãçé ã«ç½®ãã¤ã¤å¥ã®ã³ã¼ããèªã¿é²ãã¾ãããã®ç¶æ ã¯åæç¶æ ãã©ãã§å®ç¾©ããã¦ããã®ãããããã¯å ´åã«ãã£ã¦ç¶æ ã®è¨å®ã¨ããã®ã¯æ¹æ³ãå¤ãã£ã¦ããã®ãããããã®ããããã£ã¯å¤é¨ãããã©ãããå¤æ´ããã¦ããããã ããæä¸ãã ã»ã»ãªã©ã¨æãã¤ã¤èªã¿é²ãã¾ããã¾ãããããã«ã³ã¼ããèªãã¨ããã®ã¯ä½åã®ããä½æ¥ã§ãããã§ããã°ãã£ãã¨çµè«ãç¥ãããã®ã§ãããã®ã»ã³ãã³ã¹ã®ããã«ã
ãããªãã³ã¼ãã¯æ¶ãã¾ãããã使ããã¦ããªãã³ã¼ãã¯æ¶ãã¾ããããã³ã¼ããçããã¨ã¯ãå°æ¥ã®ãã¼ã ã¡ã³ãã¼ã®æéãç¯ç´ãã¾ãã
dead codeãã¤ã¾ãå¼ã°ãã¦ããªãã³ã¼ããã¾ãæ¶ããã¨ãèããã§ããããããããªãããããå°æ¥ã«æ¸¡ã£ã¦ç¿æ £ã¥ãã¦ç¶ãã¦ããªãããã°ã¾ãéèãã¼ãã¼ãã«çããè±å£ã®ããã«ãªã£ã¦ãã¾ãã¾ããããã§ã³ã¼ãã£ã³ã°ã¬ã¤ãã©ã¤ã³ã«ãã³ã¼ãã®æ¶ãæ¹ããæ¸ãã¾ãããsebastianbergmann/phpdcd ãdead codeæ¤åºã®ããã«è©¦ç¨ãã¾ããããCodeIgniterã®ç¬èªloaderç°å¢ã«ããã¦ã¯éå®çãªdead codeããæ¤åºã§ããªãã£ããããPhpStormã®inspectionã使ã£ã¦dead codeãæ¢ãã¨ããã®ãã¡ã¤ã³ã§ããã¾ããã
使ããã¦ããªãé¢æ°ãã¯ã©ã¹ãããå ´åãç§éãåãããæ段ã¯2ã¤ããã¾ãã*8
- ãã£ãã¨æ¶ã
@deprecated
ã«ãã
ç¶ç¶çã«ä½¿ããã¦ããªãé¢æ°ãã¯ã©ã¹ãæ¶ãã¦ããããã«ã¯ã©ãããããããï¼æããã«å¼ã³å
ããªãé¢æ°ã¯æ¶ãã¦è¯ãã§ãããããããPHPã®å ´åãå®è¡æã«ã¯ããã¦å¼ã°ããã¨ãããå ´åãããã¾ããä¾ãã° PHP: call_user_func - Manual ãã¤ãã£ã¦ $callback
ãåçã«æå®ããã¦ããå ´åãéç解æã§ã©ã®é¢æ°ãã³ã¼ã«ãããããæ¨å®ããã®ã¯é£ããã§ããããæ¶ãã®ãä¸å®ãªå ´åã«ã¯ PHP: trigger_error - Manual ãã¤ãã£ã¦ E_USER_DEPRECATED
ã¨ã©ã¼ãçæãã¾ãããã
<?php /** * @deprecated */ function d() { trigger_error("function d() is deprecated since 2016/12/14. use f() instead.", E_USER_DEPRECATED); }
é¢æ°ãdeprecatedã«ããå ´åã«ã¯ä¸è¨ã®ããã«ãããã¨ãã³ã¼ãã£ã³ã°ã¬ã¤ãã©ã¤ã³ã«ããã¦ãã¾ãã*9 ç§ãã¡ã¯ãããã¨ã©ã¼ãã³ãã©ãããã®ã³ã°ãããã¨ã«ãããéæ¨å¥¨ãªé¢æ°ãå¼ã°ãã¦ãããã©ãããã¢ãã¿ãªã³ã°ã§ãã¾ããã¾ãPhpStormãªã©ã®PHPDocããµãã¼ããããã¨ãã£ã¿ãã¤ãã£ã¦ããã°å¼ã³åºãå´ã®ã³ã¼ãã«warningãåºãããããå®è£ ã®æå©ãã«ããªãã¾ãã
ãã®æ¹æ³ã¯ãä»ããã«ã¯æ¶ããªããã©å°æ¥çã«ã¯ç½®ãæãããã³ã¼ããã示ãããã«ãè¯ãæ¹æ³ã§ãã
次ã®ã¹ããã
ã¨ããæãã«ãã¼ã£ã¨é²ãã¦ããã®ããã®2ã¶æã»ã©ã®åãçµã¿ã§ãããã²ã¨ã¾ãã¢ããªã±ã¼ã·ã§ã³ã¨ãã¦ã®è¶³åããæ´ãã¦ãã¾ãããã¨ã¯ãããããä»ã¾ã§ãã¼ã ã®ä¸ã§è¦éãããã¦ãã¦ããã®ãæ大ã®èª²é¡ã ãªã¨æã£ã¦ãããç¶ç¶çã«ã¢ããªã±ã¼ã·ã§ã³ãæ¹åã§ããããã«ããã«ã¯ã©ãããããããã®ãã¨ããã®ãç§ã®æè¿ã®é¢å¿ãã¨ã§ãã
解決ã§ãã¦ããªã課é¡ãæ®ã£ã¦ãã¾ããããããªãããããã¯ä¸æ°ã«è§£æ±ºã§ãããã®ã§ã¯ãªããçµç¹ã§ã¢ããã¼ããã¹ã課é¡ã ã¨èãã¦ãã¾ããä»åããã¨ã¯å¥ã«ä»¥ä¸ãé²ãã¾ããã
- PHP 5.3ç³»ããPHP 5.6ç³»ã¸ã®ãã¼ã¸ã§ã³ã¢ããã®ããã®ã³ã¼ãã®æ¸ãæã (ã³ã³ã¹ãã©ã¯ã¿æ¸ãæã / static callã®å¯¾å¿ ãªã©ãªã©)
require_once
ãæ¶ãã¦composerã®autoloadã«ç§»æ¤ããããã¯CodeIgniterã®Loaderããcomposerã®autoloadã¸ã®æ®µéçãªã©ã¤ãã©ãªã®ç§»æ¤ã- æ¢åã®Selenium baseã®ãã©ã¦ã¶ãã¹ãã¨å¥ã«ããããã¨HTTP response statusããã¹ãããããã®ä¸¦åHTTPãã¹ããã¼ã«ä½æ*10
- 使ã£ã¦ããªã
use
å¥ã®åé¤ / 使ã£ã¦ãªãã©ã¤ãã©ãªã®åé¤ã»ãããã¯åæ©è½ã®ã©ã¤ãã©ãªã¸ã®çµ±ä¸ @property
ã¢ããã¼ã·ã§ã³ãå©ç¨ãã¦ç°¡æçã«CodeIgniterã®loaderçµç±ã§å¼ã°ãã¦ããã¯ã©ã¹åã³ã¡ã½ãããinspectionãå¯è½ã«ãã
課é¡ã¯è¦ããã¨ããããã³ãã³ã解æ¶ãã¦ãããªããã°ãªãã¾ãããçµç¹ã¨ãã¦ãããã課é¡ã«å¯¾ãã¦ã©ã®ããã«å¯¾å¿ãããããããã¯ã©ã®ããã«æéãå²ãã¦ããã®ããå人çã«ã¯è¤éãªã¢ããªã±ã¼ã·ã§ã³ãå®è£ ãããã®ã¯ããç¨åº¦ããããªããªãã¨èãã¦ãã¾ããããã¦complicatedãªã¢ããªã±ã¼ã·ã§ã³ã¯ããç¨åº¦æè¡çãªã¢ããã¼ãã«ãã£ã¦æ¹åå¯è½ã§ããããäºæ¥ãæ¯ãããããã¯ãã«æ¹ãã¦æ¯ãå¹ãè¾¼ãã¨ããã®ã¯ããããã®ãããã¼ãã ã¨æã£ã¦ãã¾ãã
ãããããªã¢ããªã±ã¼ã·ã§ã³éçºã®è©±ããããã¨ããæ¹ãããã£ãããã°æ¯é #ajiting ãã¾ãããï¼ãæ°è»½ã«ã声ãããã ããã¾ãã voyagegroup.com/recruit/
ææ¥ã®ã¢ããã³ãã«ã¬ã³ãã¼ã®è¨äºãã楽ãã¿ã«ã
P.S. æå稿ã®ç· ãåãã伸ã³ãã®ã§æ°åãå ¥ãã¦è¨äºãæ¸ããã¨21æããããã¹ã¼ãã¼ãã©ã¤çæã«æ¸ãé²ãã¦ãããä¸ä¸åãè¶ ãã¦ãã¾ãåçãã¦ãã¾ã
*1:t_wadaããã«ã¯ãã¹ããä¾å¤è¨è¨ã®ãã¨ã§ãã£ãããéçºãã¼ã ã®èª²é¡ã®æãèµ·ããã§ãã£ããããã¢ããããã¼ã ã¡ã³ãã¼ã¨ãã¦ããã ããããã¦ãã¾ãã
*2:ãã¨ãã°retryå¯è½ãªä¾å¤ãªã©ã®å ´åã«ã¯ä¾å¤ã®æãç´ããããã«é常ã®å¦çãretryãããªã©
*3:ãã¡ãããæ¡ãã¤ã¶ããã¦ããä¾å¤ã«ã¤ãã¦ã¯å¤§åã®ä¾å¤ãã³ãã©ã§ææãããã¨ã¯ã§ãã¾ããã
*4:ã¡ãªã¿ã«CodeIgniter 1.7ç³»ã§ã¯PHPã¨ã©ã¼ã®ããã®å¤§åãã³ãã©ãããã¾ãããä¾å¤ã®ããã®å¤§åãã³ãã©ã¯ããã¾ããã
*5:ãã°ã¤ã³ãã¦ããã¨ãã¨ãã¦ããªãã¨ãã¨ã§ã¨ã©ã¼è¡¨ç¤ºãåããããã¨ããèãããããããè¨è¨ã«ãªã£ã¦ããããã§ããã¦ã¼ã¶ããã®ä½¿ãåæãèããã¨ããã¯æåãå¤ããªãã»ããããã ããã¨å¤æãããã®ã¾ã¾è¸è¥²ãã¾ãã
*6:ã¡ãªã¿ã«ç§ãããæ¸ãGoã®ä¸çã§ã¯DIã³ã³ããã¯ãã¾ãä¸è¬çã§ã¯ããã¾ãããtypeã¨interfaceã大æ¦ã®ä¾åé¢ä¿ãæ示ãã¦ãã¾ãããã§ããã¨ããã®ã¯æ¨ªéã«ãããã®ã§æ³¨éã§ã»ã»
*7:CodeIgniterç¨èªã§ããã¨ãã¼ã¹ãªãã¸ã§ã¯ãã«ãªãããããã¾ãããCI_Baseã®ãã¨ã§ãã
*8:æ¾ç½®ããã¨ããã®ããããã¨3ã¤ã§ãã
*9:ãã®ã¢ããªã±ã¼ã·ã§ã³ã«é¢ãã¦è¨ãã°ãã³ã¼ãã£ã³ã°ã¬ã¤ãã©ã¤ã³ããã¾ã¾ã§ãªãã£ãã®ã§ãããã®2ã¶æã®åãçµã¿ã®ä¸ã§ãããã¦ãã¢ããªã±ã¼ã·ã§ã³ã¨ãã¼ã ã«ãã£ãã³ã¼ãã£ã³ã°ã¬ã¤ãã©ã¤ã³ãä½ã£ã¦ãã¾ããããã¯ã¾ã ä½æéä¸ã§ãããã¬ãã¥ã¼ã®æéã«ããªãã¾ãããä½ãããã¼ã ã®PHPã«å¯¾ããç¥è¦ãéç´ãã¬ãã«ãå¼ãä¸ãããã¨ããæå³ãããã¾ãã
*10:ããã¯Goã® net/http ã使ãããã°ã¤ã³å¾ã®ã»ãã·ã§ã³ãæã¡åãã¤ã¤Go 1.7ã®subtestãã¤ãã£ã¦parallelã«ããããã¨HTTP responseã«å¯¾ãã¦ãã¹ãããããã¨ãããã¼ã«ã§ãã