PHPã³ã¼ãæé©åTipsã®ã¦ã½ã¨æ¬å½(解説)
PHP コード最適化 Best Practices 63+ - カタコト日記
ååã¯ãå è¨äºã«ä¸å®ã®æ¬æã表ãã¦ãé ç®ã¨ãã¯ããã¦ãã®ã¾ã¾ã«ãã¦ããã§ããã
èªåã¨ãã¦ãæ°ã«ãªãé¨åãå¤ã ãã£ãã®ã§ãå°ãã ã調ã¹ç´ãã¦åªå 度ï¼è§£èª¬ã¤ãã¾ããã
ç¬æã¨åè¦ã§ãããã½(´ã¼ï½)ã æ¤è¨¼ã¯ãã¦ã¾ãããããããªããããããªããã
åªå 度AãBãCãä¸æãéæ¨å¥¨ã«åãã¦ã¿ã¾ãããã©ããã¤ã£ããã§ãã ããã
é·ãããä»åã¯ã
åªå 度A. é »åº¦ãé«ãã使ããã - 6ã¤
01. static ã«ã§ããã¡ã½ãã㯠static ã¨ãã¦å®£è¨ããããï¼4åéãï¼
æ£ããã¯ãstatic ãªã¡ã½ããã«ã¯ãOOP ã®ã«ã¼ã«ã«å¾ã£ã¦ã¡ãã㨠static 宣è¨ãã¤ãããï¼
ã£ã¦ã¨ãã§ãããããæ¬æ¥ããã§ãªããã®ãç¡çã« static ã«ãã¡ããã£ã¦è©±ã§ã¯ãªãã¯ãã*1
à <?php public function staticMethod() {} // éçã«åä½ããã¡ã½ããï¼ã¤ã³ã¹ã¿ã³ã¹çµç±ã§å¼ã³åºãããï¼
â <?php public static function staticMethod() {} // 宣è¨ãããéçã¡ã½ããï¼ç´æ¥ãå¼ã³åºãããï¼
ä¼¼ãåçã®Tipsï¼ ã¯ã©ã¹å
ã§å®æ°ä»£ããã«ä½¿ã£ã¦ãå¤æ°ããããªããconst ã«ãããã
ï¼ã³ã³ãã¤ã«æã«1度ã ããã¼ã¹ãããå®è¡æã®ãªã¼ãã¼ãããããªããªãã¾ãï¼
04. ã«ã¼ãã®æ大å¤ã¯ãã«ã¼ããå
ãã§ã¯ãªããåãã«ã»ãããã¦ãããã
19. for æã®æ¡ä»¶å¼ã«ã¯ count($array) ã®ãããªé¢æ°ããããªããï¼å¤æ°ã«æ ¼ç´ï¼
ã«ã¼ãã®ãã³ã«è¨ç®ãããã¨ã«ãªã£ã¦ç¡é§ã ãããç´¹ä»ããã¦ãä¾ï¼
à <?php for ($j = 0; $j < count($arr); $j++) {}
â <?php for ($j = 0, $max = count($arr); $j < $max; $j++) {}
10. å¯è½ã§ããã°ãæ£è¦è¡¨ç¾ãã strncasecmpãstrpbrkãstripos ã使ããã
11. strtrï¼str_replace ã®4åéãï¼ ï¼ str_replace ï¼ preg_replace ã®é ã«éãã
50. å¯è½ãªãæ£è¦è¡¨ç¾ãããctype_alnumãctype_alphaãctype_digit ã使ããã
æ£è¦è¡¨ç¾ã¯ä¾¿å©ã ãã©è² è·ããããã£ã¦ãã¨ã§ããã
ç´¹ä»ããã¦ãä¾ï¼
à <?php if (preg_match("!^foo_!i", "FoO_")) {}
â <?php if (!strncasecmp("foo_", "FoO_", 4)) {}
ã¸ãã¼ã¨æã£ãä¾ï¼ï¼æ³¨ï¼stripos 㯠str_replace ããããªãé«éï¼
<?php $src_str = file_get_contents("BIG_FILE"); $src = array('abc', 123, 'text'); $dst = array('cba', 321, 'txet'); // BIG_FILE ä¸ã«ç½®æ対象ã¨ãªãæåãã¡ $src ãå ¥ã£ã¦ãªããã strpos() ã§æ¢ã foreach ($src as $k => $v) { // å ¥ã£ã¦ãªãå ´åï¼falseï¼ã¯ãå¦çããªãï¼unset() ãã¦ç½®æ対象ããã¯ããï¼ if (strpos($src_str, $src) === false) { unset($src[$k], $dst[$k]); } } if ($src) { $new_str = str_replace($src, $dst, $src_str); }
äºåº¦æéãªã®ã«ãã£ã¡ã®æ¹ãéãããããï¼1åã®ç½®ææä½ã«æéãããããããªå ´åã§ããããã©ï¼
ãã¡ã¤ã«å
¨ä½ã«å¯¾ãã¦ã«ã¼ãã§ããããç½®æãã¾ãã£ã¦ããããªç®æãããã°å¿ç¨ã§ãããã
39. ãã¨ããç¨æããã¦ã é¢æ°ãã¡ ãæ´»ç¨ãããã
53. ä¸æçãªãã¡ã¤ã«ãä½ãã¨ãã«ã¯ tmpfile ã tempnam ã使ããã
é¢æ°ã ããããªãã£ã¦ãããã¥ã¢ã« ãããèªãããï¼ããã«æ¸ããã¦ãã Tips ã£ã¦ã
å®ã¯ããã¥ã¢ã«ã«æ¸ããã¦ããã¨ã°ã£ãã ã£ããï¼ãã¨ãèªåã§ä½ãä½ãå§ããåã«
PEAR ã¨ã PECL ã¨ãã«ä¼¼ãã®ããªããããã§ãã¯ãä¾ã¨ãã¦æã£ã¦ã便å©ãªé¢æ°ãã¡ï¼
- file_get_contents â¦ãã¡ã¤ã«ã®å 容ãå ¨ã¦æååã«èªã¿è¾¼ã
- file_put_contents â¦æååããã¡ã¤ã«ã«æ¸ãè¾¼ã
- microtime â¦ç¾å¨ã® Unix ã¿ã¤ã ã¹ã¿ã³ãããã¤ã¯ãç§ã¾ã§è¿ã
- gettimeofday â¦ç¾å¨ã®æéãå¾ã
- convert_uuencode â¦æååã uuencode ãã
- convert_uudecode â¦uuencode ãããæååããã³ã¼ããã
- http_build_query â¦URL ã¨ã³ã³ã¼ããããã¯ã¨ãªæååãçæãã
- substr_compare ⦠æå®ããä½ç½®ããæå®ããé·ãã® 2 ã¤ã®æååã«ã¤ãã¦ããã¤ããªå¯¾å¿ã§æ¯è¼ãã
- array_walk_recursive â¦é åã®å ¨ã¦ã®è¦ç´ ã«ãã¦ã¼ã¶ã¼é¢æ°ãå帰çã«é©ç¨ãã
ãã¡ã¤ã«ã®å 容ãèªã¿è¾¼ãä¾ï¼
à <?php // æã¯ãããã£ã¦æ¸ãã¦ã $data = ''; $fp = fopen("some_file", "r"); while ($fp && !feof($fp)) { $data .= fread($fp, 1024); } fclose($fp);
â <?php $data = file_get_contents("some_file"); // ä»ã¯ãã®é¢æ°ã²ã¨ã¤ã§ï¼ãããéãï¼
18. ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¯ãã¤ã³ã¹ãã
55. ãããã°ããã¨ã㯠error_reporting (E_ALL); ã«ãã¦ãããã
17. $row['id'] 㯠$row[id] ãã7åéãã
23. æªå®ç¾©ã®ãã¼ã«ã«å¤æ°ã®ã¤ã³ã¯ãªã¡ã³ãã¯å®ç¾©ããããã®ãã9ã10åé
ãã
24. æªå®ç¾©ã®ã°ãã¼ãã«å¤æ°ã«ã¤ãã¦ãåæ§ã
ã¨ã©ã¼ã¬ãã«ã E_ALL ã E_STRICT ã§ãã¨ã©ã¼ãåºãªãããã«ãã¦ããã°ããããã¹ã¦åé¿å¯è½ã
ã¡ãªã¿ã« 17çªã$row[id] ã§ã¯ã¾ããid ãå®æ°ã¨è§£éããã¦ããããç»é²ããã¦ãããèµ°æ»ããã¾ãã
ã§ãè¦ã¤ãããªãã®ã§ããããã㨠E_NOTICE ãåãã¤ã¤æååã«è§£éãç´ãã¦ãããããã§ãã
ä¸ä½äºææ§ã®ããã®æåãåä½ã¯ãããã©ãç¡é§ã ããã£ã¦ãã¨ã§ããã*2
31. ãã£ãã·ã¥ãå©ç¨ããããé常ãã³ã³ãã¤ã«åæ° x 25ã100ï¼
åéããªãã
32. ããããã£ãã·ã¥ã«ã¤ãã¦ãmemcached ã¨ã使ãããã
58. ãã¹ãªã©ã®è¨å®ãã©ã¡ã¼ã¿ã¯é
åã« serialize ãã¦å
¥ãããã£ãã·ã¥ãã¦ãããã
ãã£ãã·ã¥ã®å¹æèªä½ã¯èª¬æä¸è¦ãã§ãã©ãããªããã©ããã©ããããã®é »åº¦ã§ã©ãã«
ãã£ãã·ã¥ãããã¿ãããªæ
å ±ã®æ¹ãæçï¼
åªå 度B. æ©ä¼ãããã° / è¿·ã£ãã使ãããªç - 5ã¤
03. echo 'æ','å'; ï¼ã«ã³ãåºåãï¼ã®æ¹ãã'æ'.'å' ï¼ãããé£çµï¼ããéãã
ãã¾ããããããªããã§ããã©ã使ãæ©ä¼ãããã°ã
08. include ã require ã§ãã¡ã¤ã«ã¯ãã«ãã¹ã§æå®ãããã
51. ãã¡ã¤ã«æå®ã¯ basename / file_exists / open_basedir ãããã«ãã¹æå®ãéãã
æè¿ã¯ããã¬ã¼ã ã¯ã¼ã¯ããªã¼ããã¼ããã¦ãããããããããã§
ãã¹ãæ¸ããã¨ããã¾ããªããªãã¾ãããZend_Loader æé«ï¼
09. ã¹ã¯ãªããéå§æé㯠time() ã§ãªã $_SERVER['REQUEST_TIME'] ã§åå¾ã
é¡ä¼¼ã®ãã®ã¨ãã¦ã
- phpversion() â PHP_VERSIONï¼å®æ°ï¼
- php_uname(âsâ) â PHP_OSï¼å®æ°ï¼
- php_sapi_name() â PHP_SAPIï¼å®æ°ï¼
ãªã©ãªã©ã$_SERVER ã¨ããå®ç¾©æ¸ã¿å®æ° ããããªãé¢æ°ãå¼ã°ãã«ãã¡ãã使ããã
20. ã¡ã½ããå
ã§ã¯ãã¼ã«ã«å¤æ°ãã¤ã³ã¯ãªã¡ã³ãããã®ãä¸çªéãã
21. ã°ãã¼ãã«å¤æ°ã®ã¤ã³ã¯ãªã¡ã³ãã¯ãã¼ã«ã«å¤æ°ãã2åé
ãã
22. ãªãã¸ã§ã¯ãå¤æ°ï¼ä¾ï¼$this->v++ï¼ã®ã¤ã³ã¯ãªã¡ã³ãã¯ãã¼ã«ã«ãã3åé
ãã
ããã£ã¤ããã¤ã³ã¯ãªã¡ã³ããããã¨ãã¯ãã¼ã«ã«å¤æ°ã«å
¥ãã¦ããã
28. ããã«ã¯ã©ã¼ã ãã ã·ã³ã°ã«ã¯ã©ã¼ã ã®æ¹ãè¥å¹²éãã
é度ã®ããã¨ããããã¯ãã¯ã©ã¼ãã®ä½¿ãåãã¯æ®æ®µãããã¡ãã¨ããæ¹ãããããªã¨ã
æå³ãéããå¯èªæ§ãå¤ãã£ã¦ããããã ã¨ãåèã«æ¸ãæ¹ãçµ±ä¸ãã¦ã¯ãããã
åªå 度C. å®ã¯ã»ã¨ãã©å¹æããªããã©ã - 1ã¤
02. echo ã®æ¹ã print ããéãã
print ã¯è¿ãå¤ãã»ããããããããããã ãã©ããã®å·®ã¯ã»ã¨ãã©ç¡è¦ã§ããã¿ããã*3
éã«ãob_start ã使ã£ã¦ãã¨ã㯠print ã®æ¹ãéãã£ã¦è©±ãããã¿ããã§ããã*4
èè¶³ï¼ print ããã "éã" echo ã§ããã©ããã¼ã¿ã®åºåèªä½ããããããããé ãå¦çã§ãã
echo ãé£çºãããããªæ©ä¼ãä»®ã«ãããªããob_start ã¨ãã§ãããã¡ãªã³ã°ãã¦ã¾ã¨ãã¦
åºåããã®ãããã¿ããã§ããããã¬ã¼ã ã¯ã¼ã¯ã¨ã使ã£ã¦ããªãèªåã§ãããªã£ã¦ãã¨æãã
åªå 度ä¸æ. å¹æããã¾ãã¡ããããªãã§ãã誰ã説æ / æ¤è¨¼ãã¦ãã ãã (ï¾Ðï½)シクシク - 3ã¤
05. 大ããé åã®ãããªå¤æ°ã¯ unset() ãã¦ã¡ã¢ãªã解æ¾ãããã
æ£ãããã£ã¦ããã®ã¯ããããã§ãã大ãããã£ã¦ã©ããããâ¦ï¼DB ã¯ã¨ãªã®çµæã¨ãï¼
Web ã¢ããªã ã¨ã¡ã¢ãªã®è§£æ¾ã¨ãããã¾ãæèããªããã§ããã©ããã¡ãªçãªãã§ããããã
07. require_once ã¯ãã¤ã³ã¹ããªã®ã§ãã
52. require_once ãã require ã使ãããï¼opcode ãã£ãã·ã¥ããã¿ã®çç±ã§ï¼
å
ãã¿ã«ã¯ require ãã3ã4åé
ãã¨ããã¾ãããå¥ã®ãã³ããã¼ã¯ ã§ã¯
çéã®çµæãåºã¦ã¾ããé度ããã¬ããããã£ã±ãç¨éã«ãã£ã¦ä½¿ãåããã¹ãã§ã¯ï¼
16. å¦çãçµãã£ãããã¼ã¿ãã¼ã¹ã®æ¥ç¶ã¯åã£ã¦ãããã
ãããªãã¨çµæ§ãéããåºããã§ããããï¼
ã¾ãæ¥ç¶ããããªã£ã¡ãã£ããåæ¥ç¶ã³ã¹ãã®æ¹ã大ãããªã£ãããã¦ã
éæ¨å¥¨ï¼ é度ã®ä»£åã大ããããããã⦠- 3ã¤
06. ãã¸ãã¯ã¡ã½ããï¼ä¾ï¼ __get, __set, __autoloadï¼ã¯ä½¿ç¨ãé¿ãããã
é度ãæ±ãã¦æ©è½ã¨ãå¹çãç ç²ã«ãã¦ãæããâ¦ãæ ¹æ ãæ¢ãããã©è¦ã¤ããã詳細ä¸æã
å¥ã®ãã³ããã¼ã¯ï¼__autoload vs. require_onceï¼ã§ã¯ã__autoload ãéãã£ãã¨ãã£ã¦ã®ããã£ããã
ããã§ãªãã¦ããããã§ä¾¿å©ã«ãªã£ã¦çç£æ§ãä¸ãããªãæ°ã«ãã使ã£ã¡ããã°ããã¨æãã
33. if (strlen($foo) < 5) ã調ã¹ãããªã if (!isset($foo{5})) ã¨æ¸ãã¨éãã
ã¸ãã¼ã¨ä¸ç¬æã£ããã©ã1å¹´å¾ãããã«è¦ãããã¶ãæå³ä¸æãªãã ãããªãã
ã½ã¼ã¹ããæ常çã«ãã¸ãã¯ãèªã¿åããã£ã¦çµé¨çã«ããã大äºãªãã¨ã ã¨æãã¾ãã
追è¨ï¼ã§ãã£ã¦ããã«ããã¤ãã ã¨æå¾
ã©ããåä½ããªããããã§ãã
34. $i++ ãã ++$i ã®æ¹ãéããï¼ãããã opcode optimizer ãèµ°ã£ã¦ãå ´åï¼
ããã£ãã ã½(*´âï½)ããã£ã¦ãããªã $i++ ã ++$i ã«ç½®æããããã¡ããã¡ã§ããã
æèã«ãã£ã¦ã¯ã¾ã£ãã å¥ç© ãªã®ã§ããããæå³ã®æ±²ã¿åãã妨ããå¾®å¦ãª Tips ããªãã
++$i ãããã§å®è£
ãã¹ããã¸ãã¯ãåæ ãããã®ããé度ãæ±ãããã¾ããªããªã®ããææ§ã«ãªããã
ãã¨ã¯ç¬æã§çç¥ãï¼ãµã¼ããDBãè¨è¨ã¨ãï¼ããããããªã大ããªãããã¯ã ã¨æããã ãã©ã
å
è¨äºã«ãªã¹ãã¢ããããã¦ãã®ã¯ããªãç²ãå¯ãéãçãªå°è±¡ãåããã®ã§ã
ãµã¼ãã®ãã¥ã¼ãã³ã°ã¯ç¹å¹è¬ã«ãªãããããDB ã¾ããï¼è¨è¨ã¨ã SQLï¼ã¯å®éã«
ããã«ããã¯ã«ãªã£ã¦ããã¨ãå¤ãã¨æããããã¾ã¨ããããã¨å½¹ã«ç«ã¤ã¨æããã ãã©ã
誰ããã£ã¦ãããªãããªãããã£ã¡ç³»ã«å¼·ãæ¹ã
ã人ã¾ããã§ããã§ããã( ´ã¼ï½)y-~~
ãã¨ãååã®ã¨ã³ããªã¼ã«ãã©ãã¯ããã¯ãããã ãã
http://d.hatena.ne.jp/hanageman/20080523
http://d.hatena.ne.jp/gegegen/20080524/1211622711
ä¸ã«æããç´°ãããã¯ããã¯ããããä¸ã¤ä¸ã®ã¬ã¤ã¤ã¼ã®è©±ãªã©ãããããããã£ãã§ãã
ä»åãåèã«ããè¨äºï¼ååã¨éè¤ï¼
PHP & Performance By: Ilia Alshanetsky (PDF)
PHP & Performance By: Ilia Alshanetsky (PDFãã¿ã¤ãã«åãã§ããå¥ç©)
Optimizing PHP Through Habits - Joakim Nygård
http://phplens.com/lens/php-book/optimizing-debugging-php.php
ã¡ãªã¿ã«ã大å ã®ãã¿ã¨ãã¦ããããªãµã¤ãã§å¼ç¨ããã¾ããã® Ilia Alshanetsky ããã¯
PHP éçºãã¼ã ã®ä¸ã®äººã¿ããã§ãããã¼ãã¨ãã£ã¨ã
*1:ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨éçã¡ã½ããã®ã話ã5/27 ä¾ã®é¨åãè¨æ£ ã³ã¡ã³ãæ¬ããã²è¦ã¦ãã ããã
*3:http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
*4:http://phplens.com/lens/php-book/optimizing-debugging-php.php