PHP帝国の逆襲!(を願うPHPerが話す最近のPHPについてのクイックツアー PHP7対応版)

271fad8d53cd1f12f2b4b6d38e3d7bd3?s=47 uzulla
August 20, 2015
190

PHP帝国の逆襲!(を願うPHPerが話す最近のPHPについてのクイックツアー PHP7対応版)

YAPC::Asia tokyo 2015
uzulla

271fad8d53cd1f12f2b4b6d38e3d7bd3?s=128

uzulla

August 20, 2015
Tweet

Transcript

  1. 11.
  2. 15.
  3. 16.
  4. 17.
  5. 18.
  6. 19.
  7. 20.
  8. 30.

    Apacheͷઃఆ # httpd.conf౳ LoadModule php5_module modules/libphp5.so <FilesMatch ".+\.php$"> SetHandler application/x-httpd-php

    </FilesMatch> # .htaccess౳ RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L]
  9. 31.

    ༨ஊɺҰߦAddHandler͸ηΩϡϦςΟతʹϠό ͍ # httpd.conf౳ AddHandler php5-script .php • Α͘ݟ͔͚Δهड़͕ͩ… •

    http://d.hatena.ne.jp/tmatsuu/ 20150221/1424531513 • AddHandler͸ผʹʮ֦ுࢠʯͰͳ͍ͷ Ͱɺ.php͕ϑΝΠϧ໊Ͳ͜ʹ͸͍ͬͯͯ΋PHP ͱͯ͠ղऍ͞ΕΔ᠘
  10. 33.

    nginxͷίϯϑΟάྫ index index.php try_files $uri $uri/ $uri/index.php /index.php; location ~

    \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # !ͷઃఆϑΝΠϧͷதʹେྔͷઃఆ͕Ӆ͞Ε͍ͯΔ…ʂ include fastcgi_params; } • ղΓͮΒ͍ʂʂʂʂʂ • ͜Ε͚ͩͰ͸μϝͳࣄ΋͋Δ
  11. 36.

    h2o+php-fpm file.custom-handler: extension: .php fastcgi.connect: host: 127.0.0.1 port: 9000 type:

    tcp hosts: "doraperson2015.yapcasia.org:443": paths: /: file.dir: /var/www/dora-person-election/app/htdocs redirect: url: /index.php/ internal: YES status: 307
  12. 37.

    h2o+php- cgi(fastcgi.spawn) file.custom-handler: extension: .php fastcgi.spawn: "PHP_FCGI_CHILDREN=10 exec /path/to/php-cgi" •

    phpϓϩηεͷੈ࿩Λh2oʹ·͔ͤΒΕΔʂ • php-fpmෆཁɺ؂ࢹ఺͕ݮΔ! • ଎౓΋ɺͺͬͱݟphp-fpmͱḮ৭ͳͦ͞͏ • php-fpm.confͳͲΛॻ͔ͳͯ͘΋ྑ͍"
  13. 39.

    h2oॴݟ ଟ෼͜Ε͔Βͷ఺ • .htaccess͸ແ͍ͷͰɺڽͬͨઃఆͩͱ΍ͬͺ Γ௕͍ • try_filesతͳ֓೦͸Θ͔Βͳ͍ͱ… • ͽͪͺʔʹ͸Ϗϧυ͕໘౗ •

    yumͱ͔aptͱ͔΄͍͠ΑͶ͆ • ઃఆͷൿ఻ͷͨΕ͸·ͩগͳΊɺಛʹӡ༻ • ߏ੒͕গʑಠಛɺinit.dͳͲͲ͏͢Δʁ
  14. 43.

    ͞Βʹ༨ஊ • PHP͸PaaS΋;͖͑ͯͯ·͢ • Heroku • Azure app service •

    Google App Engine • ͍ͣΕ΋ਖ਼ࣜʹʢͱ͸͍͑ɺ·ͩPHP5.5ɺ. 6͘Β͍ʣ
  15. 44.
  16. 50.
  17. 53.

    Type declarations(ܕએݴ) • ݩType Hinting • Ҿ਺΍ฦ஋ͷʮܕʯΛࢦఆ • ʢPHPͷʮܕʯͰ͢ʣ •

    ʮJava͍ʯ • ҧ൓Ͱྫ֎ʢPHP<7͸Τϥʔʣ • 7͔ΒεΧϥ஋ΛࢦఆՄೳʹ㊗ • 7͔Βฦ஋΋ࢦఆՄೳʹ"
  18. 54.

    ଴๬ͷεΧϥ஋͕ࢦఆՄೳʹʂ <?php function func(int $num){ } func('a'); // => TypeError

    Exception !" func('1'); // => OK #$ • ʮ͔͠͠'1'͸௨Δʯ • ʢօ͞ΜʣʮOK͡ΌͶ͑Α͆͜Ε͔ͩΒ PHPer͸͆͆͆ʯ
  19. 56.

    strict_types <?php // PHP7.0.0beta2ʹͯ֬ೝ declare(strict_types=1); // ! function func(int $num){

    /* ʙ */} func('1'); // => TypeError " func(1.0); // => TypeError "" func(true); // => TypeError """ • ઌ಄ͷdeclare(strict_types=1);Ͱݫ֨ʹ • float=>int❌ͳͷɺڻ͖ͷϚοτ΢ͳڍಈ͆
  20. 58.

    ࣗಈΩϟετͰ͜Ε͕ΤϥʔʹͳΔ <?php declare(strict_types=1); func(1); // !΋ͱ΋ͱͷintͳҾ਺͕ function func(float $num){ //

    "͜͜ͰfloatʹͳΓ func2($num); // #TypeErrorʹͳΔ } function func2(int $num){ // $func2ͷҾ਺͸intࢦఆͳͷͰ } • int->float͸Ωϟετ͞ΕΔ! • ·͋ɺPHPΒ͍͠Ͱ͢Ͷ"
  21. 60.

    ࢒೦ʁ஫ҙʁϙΠϯτ <?php function func(){ $a = 1; return function ()

    use ( int $a ) { // Error return $a*2; } } • use۟ʹ͸ʢRC1ͷݱ࣌఺ͰʣܕએݴͰ͖ͳ͍
  22. 62.

    ͜͏͍͏ͷ͸ࢦఆͰ͖ͳ͍ • ❌ : (function(int): int) • (HackͳΒͰ͖Δ) • ❌

    : \DateTime[] • PHPDocͰ͓ͳ͡Έɺཉ͍͠… • ❌ : mixed • ·͊ɺॻ͔ͳ͚Ε͹౳Ձ
  23. 63.

    ࢖ͬͨ࣌ɺ଎౓͸Ͳ͏ͳͷʁ • ༗Γ 0.4381 sec • ແ͠ 0.3516 sec •

    ࠩ 0.0865 sec(86.5ms) / Ұઍສճ • MBP i7+PHP7β2Ͱɺ10,000,000(Ұઍສ)ճ͠ ͯ͜ͷఔ౓ • (PHPͷ༻్Ͱ͸)࣮༻্ޡࠩͳͷͰؾܰʹ࢖͍ ͍ͨɻ
  24. 64.

    ࢼͨ͠ίʔυ <?php declare(strict_types=1); function func (int $num): int{ return $num;

    } // ༗Γ function func2 ($num) { return $num; } // ແ͠ $start = microtime(true); for($i=0;$i<10000000;$i++){ func($i); // OR func2($i); } echo microtime(true) - $start; • ͪͳΈʹɺis_intͰࣅͨΑ͏ͳίʔυॻ͍ͨΒ ଎౓͕΄΅ಉͩͬͨ͡
  25. 65.
  26. 72.

    PHPͷҋਂ͖Errorʹ͍ͭͯ 4 • ͨͩ͠ɺக໋తͳΤϥʔ͸ଈࢮͰϋϯυϧͰ͖ ͳ͍(PHP<7) • register_shutdown_functionΛ͔ͭͬͨ τϦοΫ͕ඞཁʢ෮ؼ΋Ͱ͖ͳ͍ʣ register_shutdown_function( function(){

    $e = error_get_last(); if( $e['type'] == E_ERROR || $e['type'] == E_PARSE || $e['type'] == E_CORE_ERROR || $e['type'] == E_COMPILE_ERROR || $e['type'] == E_USER_ERROR ){ // ͓޷͖ͳॲཧΛॻ͘ echo "க໋తͳΤϥʔ͕ൃੜ͠·ͨ͠ɻ\n"; echo "Error type:\t {$e['type']}\n"; echo "Error message:\t {$e['message']}\n"; echo "Error file:\t {$e['file']}\n"; echo "Error line:\t {$e['line']}\n"; } } );
  27. 83.

    PHP7͔Β͸ɺக໋తΤϥʔ΋ྫ֎ͰϦΧόϦʔ Ͱ͖Δ • \Error ΫϥεΛ਌ͱͨ͠Ϋϥε͕ྫ֎Ͱ౤͛ ΒΕΔ • க໋తͳΤϥʔͷʮେମશ෦ʯ͕ʮྫ֎ʯʹͳ Δ •

    ͳͬͯͳ͍ॴ΋͋Δʢྫɿ࠷ॳͷίʔυͰ ͸ύʔεΤϥʔΩϟονෆೳ౳ʣ • ࠷ޙ·ͰΩϟον͞Εͳ͔ͬͨྫ֎͸ैདྷͷΤ
  28. 85.

    ྫ <?php try{ ଘࡏ͠ͳ͍ؔ਺(); }catch(\Excepion $e){ // ௨ৗͷྫ֎͸͜͜ʹ }catch(\Error $e){

    // ৽͍͠ɺΤϥʔʢͷྫ֎ʣ͸͜͜ʹ } // PHP<7 ͸͜͜ʹ͘Δ͜ͱ͸ෆՄೳ
  29. 90.
  30. 92.

    ??ԋࢉࢠ • ΈΜͳେ޷͖isset͔Βͷ୤٫ if(isset($_GET['name'])) $name = $_GET['name']; else $name =

    'anonymous'; ! $hoge = (isset($_GET['name'])) ? $_GET['name'] : 'anonymous'; ! $name = $_GET['name'] ?? 'anonymous' ; • ??͸ະఆٛม਺ʹ৮ͬͯ΋େৎ෉Ͱ͢ɺศ རʂʂʂʂ
  31. 93.

    <=> UFOԋࢉࢠ • Rubyͷ͋ΕͰ͢ɺιʔτ͕ศརʹʂʂ <?php $item_list = [ ["id"=>3, "name"=>"uzulla"],

    ["id"=>2, "name"=>"lestrrat"], ["id"=>10, "name"=>"moznion"] ]; usort($item_list, function ($left, $right) { return $left["id"] <=> $right["id"]; });
  32. 94.

    ෳ਺ΧϥϜ΋ࢦఆͰ͖·͢ <?php usort($item_list, function ($left, $right) { return [$left["id"], strlen($left["name"])]

    <=> [$right["id"], strlen($right["name"])] ; }); • ͜Ε͸SQLͷෳ਺ORDERͬΆͯ͘ศརͬΆ͍
  33. 96.

    ྫ <?php $app->setLogger( new class extends \My\Logger { public function

    emergency( $message, array $context = [] ){ \My\AlertPush($message); parent::emergency($message, $context); } } ); • ͜Ε͸֦ுͯ͠Δ͚ͲɺѲΓͭͿ࣌͢ͱ͔ʢʣ ʹ΋࢖͑ͦ͏Ͱ͢Ͷ
  34. 97.

    ༧໿ޠ͕௥Ճ • Ϋϥε໊/ΠϯλʔϑΣΠε໊ • ஍ຯʹΠϯύΫτ͕͋Δ • int, float, bool, string,

    true, false, null, resource, object, mixed, numeric • Ұ෦ͷWAFʹ͸ɺStringͱ͍͏Ϋϥε͕…
  35. 114.
  36. 118.
  37. 122.
  38. 130.
  39. 133.
  40. 139.
  41. 140.
  42. 141.
  43. 142.
  44. 143.
  45. 144.
  46. 145.
  47. 147.

    ԶతʮͲͬͪΛ࢖͏΂͖͔ʁʯ in 2015 • ։ൃ΋ຊ൪΋Linux͔ʁ • ͍͍͑ → PHP •

    ͸͍ → • ผʹʁ → PHP • HackΛ࢖͍͍ͨ → HHVM
  48. 151.

    ҰԠɺHackͷΞπ͞ • Hack language • asyncʢMysql΍memcacheʣ • Generics • Type

    Aliasing • ==> Ͱλ • Type check جຊඞਢ http://docs.hhvm.com/manual/en/ install.hack.bootstrapping.php
  49. 152.

    HackΛ஌Βͳ͍ਓ޲͚ɺ==> ʹ͍ͭ ͚ͯͩαϯϓϧ <?hh // strict function l() : (function(int):

    int) { $a = 1; return $b ==> $a + $b; } $f = l(); echo $f(2); // => 3 • ΦγϟϨͩ͠ɺʢΑ͘σΟεΒΕΔʣuse͕ෆ ཁ
  50. 153.

    ૉ๿ͳPHPͰॻ͘ͱ͜͏͍͏ίʔυ <?php function l() { $a = 1; return function

    ($b) use ($a){ return $a + $b; } } $f = l(); echo $f(2); // => 3
  51. 154.

    ͱ͜ΖͰɺPHP7Ͱ͸ʁ <?php declare(strict_types=1); function l(): Closure { $a = 1;

    return function (int $b) use ($a) : int{ return $a + $b; } } $f = l(); echo $f(2); // => 3 • ※rc1Ͱuse۟ͷܕએݴ͸͏͔ͣ͝…
  52. 156.