Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

uzulla
August 20, 2015
440

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

YAPC::Asia tokyo 2015
uzulla

uzulla

August 20, 2015
Tweet

More Decks by uzulla

Transcript

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

    http://d.hatena.ne.jp/tmatsuu/ 20150221/1424531513 • AddHandler͸ผʹʮ֦ுࢠʯͰͳ͍ͷ Ͱɺ.php͕ϑΝΠϧ໊Ͳ͜ʹ͸͍ͬͯͯ΋PHP ͱͯ͠ղऍ͞ΕΔ᠘
  3. 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; } • ղΓͮΒ͍ʂʂʂʂʂ • ͜Ε͚ͩͰ͸μϝͳࣄ΋͋Δ
  4. 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
  5. 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ͳͲΛॻ͔ͳͯ͘΋ྑ͍"
  6. h2oॴݟ ଟ෼͜Ε͔Βͷ఺ • .htaccess͸ແ͍ͷͰɺڽͬͨઃఆͩͱ΍ͬͺ Γ௕͍ • try_filesతͳ֓೦͸Θ͔Βͳ͍ͱ… • ͽͪͺʔʹ͸Ϗϧυ͕໘౗ •

    yumͱ͔aptͱ͔΄͍͠ΑͶ͆ • ઃఆͷൿ఻ͷͨΕ͸·ͩগͳΊɺಛʹӡ༻ • ߏ੒͕গʑಠಛɺinit.dͳͲͲ͏͢Δʁ
  7. ͞Βʹ༨ஊ • PHP͸PaaS΋;͖͑ͯͯ·͢ • Heroku • Azure app service •

    Google App Engine • ͍ͣΕ΋ਖ਼ࣜʹʢͱ͸͍͑ɺ·ͩPHP5.5ɺ. 6͘Β͍ʣ
  8. Type declarations(ܕએݴ) • ݩType Hinting • Ҿ਺΍ฦ஋ͷʮܕʯΛࢦఆ • ʢPHPͷʮܕʯͰ͢ʣ •

    ʮJava͍ʯ • ҧ൓Ͱྫ֎ʢPHP<7͸Τϥʔʣ • 7͔ΒεΧϥ஋ΛࢦఆՄೳʹ㊗ • 7͔Βฦ஋΋ࢦఆՄೳʹ"
  9. ଴๬ͷεΧϥ஋͕ࢦఆՄೳʹʂ <?php function func(int $num){ } func('a'); // => TypeError

    Exception !" func('1'); // => OK #$ • ʮ͔͠͠'1'͸௨Δʯ • ʢօ͞ΜʣʮOK͡ΌͶ͑Α͆͜Ε͔ͩΒ PHPer͸͆͆͆ʯ
  10. 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❌ͳͷɺڻ͖ͷϚοτ΢ͳڍಈ͆
  11. ࣗಈΩϟετͰ͜Ε͕ΤϥʔʹͳΔ <?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Β͍͠Ͱ͢Ͷ"
  12. ࢒೦ʁ஫ҙʁϙΠϯτ <?php function func(){ $a = 1; return function ()

    use ( int $a ) { // Error return $a*2; } } • use۟ʹ͸ʢRC1ͷݱ࣌఺ͰʣܕએݴͰ͖ͳ͍
  13. ͜͏͍͏ͷ͸ࢦఆͰ͖ͳ͍ • ❌ : (function(int): int) • (HackͳΒͰ͖Δ) • ❌

    : \DateTime[] • PHPDocͰ͓ͳ͡Έɺཉ͍͠… • ❌ : mixed • ·͊ɺॻ͔ͳ͚Ε͹౳Ձ
  14. ࢖ͬͨ࣌ɺ଎౓͸Ͳ͏ͳͷʁ • ༗Γ 0.4381 sec • ແ͠ 0.3516 sec •

    ࠩ 0.0865 sec(86.5ms) / Ұઍສճ • MBP i7+PHP7β2Ͱɺ10,000,000(Ұઍສ)ճ͠ ͯ͜ͷఔ౓ • (PHPͷ༻్Ͱ͸)࣮༻্ޡࠩͳͷͰؾܰʹ࢖͍ ͍ͨɻ
  15. ࢼͨ͠ίʔυ <?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ͰࣅͨΑ͏ͳίʔυॻ͍ͨΒ ଎౓͕΄΅ಉͩͬͨ͡
  16. 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"; } } );
  17. PHP7͔Β͸ɺக໋తΤϥʔ΋ྫ֎ͰϦΧόϦʔ Ͱ͖Δ • \Error ΫϥεΛ਌ͱͨ͠Ϋϥε͕ྫ֎Ͱ౤͛ ΒΕΔ • க໋తͳΤϥʔͷʮେମશ෦ʯ͕ʮྫ֎ʯʹͳ Δ •

    ͳͬͯͳ͍ॴ΋͋Δʢྫɿ࠷ॳͷίʔυͰ ͸ύʔεΤϥʔΩϟονෆೳ౳ʣ • ࠷ޙ·ͰΩϟον͞Εͳ͔ͬͨྫ֎͸ैདྷͷΤ
  18. ྫ <?php try{ ଘࡏ͠ͳ͍ؔ਺(); }catch(\Excepion $e){ // ௨ৗͷྫ֎͸͜͜ʹ }catch(\Error $e){

    // ৽͍͠ɺΤϥʔʢͷྫ֎ʣ͸͜͜ʹ } // PHP<7 ͸͜͜ʹ͘Δ͜ͱ͸ෆՄೳ
  19. ??ԋࢉࢠ • ΈΜͳେ޷͖isset͔Βͷ୤٫ if(isset($_GET['name'])) $name = $_GET['name']; else $name =

    'anonymous'; ! $hoge = (isset($_GET['name'])) ? $_GET['name'] : 'anonymous'; ! $name = $_GET['name'] ?? 'anonymous' ; • ??͸ະఆٛม਺ʹ৮ͬͯ΋େৎ෉Ͱ͢ɺศ རʂʂʂʂ
  20. <=> 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"]; });
  21. ෳ਺ΧϥϜ΋ࢦఆͰ͖·͢ <?php usort($item_list, function ($left, $right) { return [$left["id"], strlen($left["name"])]

    <=> [$right["id"], strlen($right["name"])] ; }); • ͜Ε͸SQLͷෳ਺ORDERͬΆͯ͘ศརͬΆ͍
  22. ྫ <?php $app->setLogger( new class extends \My\Logger { public function

    emergency( $message, array $context = [] ){ \My\AlertPush($message); parent::emergency($message, $context); } } ); • ͜Ε͸֦ுͯ͠Δ͚ͲɺѲΓͭͿ࣌͢ͱ͔ʢʣ ʹ΋࢖͑ͦ͏Ͱ͢Ͷ
  23. ༧໿ޠ͕௥Ճ • Ϋϥε໊/ΠϯλʔϑΣΠε໊ • ஍ຯʹΠϯύΫτ͕͋Δ • int, float, bool, string,

    true, false, null, resource, object, mixed, numeric • Ұ෦ͷWAFʹ͸ɺStringͱ͍͏Ϋϥε͕…
  24. ԶతʮͲͬͪΛ࢖͏΂͖͔ʁʯ in 2015 • ։ൃ΋ຊ൪΋Linux͔ʁ • ͍͍͑ → PHP •

    ͸͍ → • ผʹʁ → PHP • HackΛ࢖͍͍ͨ → HHVM
  25. ҰԠɺHackͷΞπ͞ • Hack language • asyncʢMysql΍memcacheʣ • Generics • Type

    Aliasing • ==> Ͱλ • Type check جຊඞਢ http://docs.hhvm.com/manual/en/ install.hack.bootstrapping.php
  26. HackΛ஌Βͳ͍ਓ޲͚ɺ==> ʹ͍ͭ ͚ͯͩαϯϓϧ <?hh // strict function l() : (function(int):

    int) { $a = 1; return $b ==> $a + $b; } $f = l(); echo $f(2); // => 3 • ΦγϟϨͩ͠ɺʢΑ͘σΟεΒΕΔʣuse͕ෆ ཁ
  27. ૉ๿ͳPHPͰॻ͘ͱ͜͏͍͏ίʔυ <?php function l() { $a = 1; return function

    ($b) use ($a){ return $a + $b; } } $f = l(); echo $f(2); // => 3
  28. ͱ͜ΖͰɺ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۟ͷܕએݴ͸͏͔ͣ͝…