Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Perlに比べてPHPが不便(主観です) ああ…だから僕は…
Search
uzulla
May 21, 2016
Programming
3
4.6k
Perlに比べてPHPが不便(主観です) ああ…だから僕は…
LT at PHP Conference Fukuoka 2016
uzulla
May 21, 2016
Tweet
Share
More Decks by uzulla
See All by uzulla
セッションのトークセッション / Traps for PHP session features in growing web apps
uzulla
2
83
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
5
1.8k
例外を投げるのをやめてみないか? あるいは受け入れてみないか? - How to use exceptions other than throwing
uzulla
4
770
PHPerが ISUCONでやるべき事
uzulla
1
590
開発生産性は上がらない - N Ways to Reduce Development Productivity
uzulla
1
180
test is not a job
uzulla
1
450
あなたのPHPアプリ、ログはでてますか?あるいはログをだしてますか? / Are you writing a log? Or just out a log?
uzulla
16
7k
有名FWを採用する?しない? 令和5年におけるオレオレフレームの立ち位置 / To make with own framework or not do, that is the question
uzulla
2
1k
May the PHP dev be with xdebug
uzulla
0
150
Other Decks in Programming
See All in Programming
Appleの新しいプライバシー要件対応: ノーコードアプリ プラットフォームの実践事例
nao_randd
1
460
From Spring Boot 2 to Spring Boot 3 with Java 22 and Jakarta EE
ivargrimstad
0
1k
開発を加速する共有Swift Package実践
elmetal
PRO
0
330
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
1
1k
暴走のウホーレン 〜想いってのはvimrcにしないと伝わらないんだぜ〜 / iosdc_japan_2024
uhooi
1
240
いつか使える ObjectSpace / Maybe useful ObjectSpace
euglena1215
2
120
Regular Expressions, REXML, Automata Learning
makenowjust
0
190
マルチモジュールにおけるテスト最適化
fxwx23
0
180
Rustではじめる負荷試験
skanehira
5
1.2k
Architecture Decision Record (ADR)
nearme_tech
PRO
1
220
New Order in Cascade Sorting Order
mugi_uno
3
2.5k
月間4.5億回再生を超える大規模サービス TVer iOSアプリのリアーキテクチャ戦略 - iOSDC2024
techtver
PRO
1
620
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
21
3k
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
88
16k
5 minutes of I Can Smell Your CMS
philhawksworth
201
19k
Code Review Best Practice
trishagee
62
16k
How GitHub (no longer) Works
holman
309
140k
A Modern Web Designer's Workflow
chriscoyier
690
190k
Building Your Own Lightsaber
phodgson
101
6k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
Six Lessons from altMBA
skipperchong
26
3.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
VelocityConf: Rendering Performance Case Studies
addyosmani
321
23k
Transcript
PerlʹൺͯPHP͕ෆศ ʢओ؍Ͱ͢ʣ ͋͋…͔ͩΒ… • date: 20160521 • at: PHPΧϯϑΝϨϯεԬ 2016
• speaker: uzulla
ࣗݾհ • ౦ͷํͷ͏ͣΒ(uzulla)Ͱ͢ • ମܕతʹʮLʢΤϧʣʯͳํ Ͱ͢ • ʢԬͷudzura͞Μମܕ ʮRʢϨΪϡϥʔʣʯͰ͢ Ͷʣ
• Perlք۾ͰPHPͷਓͱ͍Θ Ε·͕͢ɺPHPք۾ͰPerl ͷਓͱ͍ΘΕ͍ͯΔؾ͕͠· ͢
Perlʹൺͯ PHP͕ෆศ ʢओ؍Ͱ͢ʣ
ʮPerlͷΧοίΠΠϠπ ʢओ؍ʣ ΛPHPͰ͔͍͍ͭͨ ͧʂʂʯ
͋͋…͔ͩΒ…
ΠΤʔΠʂ
yay • h#ps:/ /github.com/marcioAlmada/yay • Yay is a high level
PHP preprocessor • Not ready for real world usage yet !
ϓϦϓϩηοαͱʁ • Cͱ͔Ͱ͓ೃછΈ • ίϯύΠϧ࣌ʹίʔυΛಈతʹม • ఆɺϚΫϩஔ • yayPHPίʔυΛม͠ɺPHPίʔυͱͯ͠ग़ྗ ͢ΔτϥϯεύΠϥΈ͍ͨͳͷ
• ࠓճPHPͷߏจΛվ͢Δͷʹ͔͍ͭ·͢
ͱΓ͋͑ͣࢼ͢πʔϧ • github.com/uzulla/yay-tester.php • evalͯ͠ΔͷͰةݥͰ͢!खݩͰ͔͍ͭ·͠ΐ͏ • ʮ҆શͳϨϯαόͳΒ͏͔͍͍ͯ͝͠…ʁʯ ʢԥΒΕͦ͏ͳͷͰࣗॏ͠·ͨ͠ʣ • PHP>=7
ʢevalΛtryʙcatch (\Throwable $e) {}Ͱ ଈࢮճආʣ
say • say "hogehoge"; • sayechoΑΓҰจࣈগͳ͍ʂ • վߦ͕͘ͷ͕࠷ߴʹศརʂ • ʮPerl6͡ΌΜʯ
ʮp5Ͱuse feature 'say'͢Ε…ʯ
<?php # ͜ͷʮmacroʯͱ͍͏ॴΛyay͕ࢀর͢Δ macro { say ·string()·message } >> {
echo ·message; echo PHP_EOL; } # ͜͜·Ͱmacro say "say!"; say "yes!";
say "say!"; say "yes!"; // ↓ echo "say!"; echo PHP_EOL;
echo "yes!"; echo PHP_EOL;
say! yes!
unless • unless($flag){} • if(!$flag){}ͱಉ͡ • ʮ!Λॻ͚͍͍͡Όͳ͍ʯ ʮif( ! isKakin())Έ͍ͨͳͷࢁଓ͘ͱͭΒ͍…ʯ
ʮisHikakin()Λͭ͘Ε͑͑Μʯ ʮͳΔ΄Ͳʁʯ
<?php macro { unless (···expression) { ···body } } >>
{ if (! (···expression)) { ···body } } $isSuccess = true; unless($isSuccess){ echo('failed'); }
$isSuccess = true; unless($isSuccess){ echo('failed'); } // ↓ $isSuccess =
false; if (! ($isSuccess)) { echo('failed'); }
failed
range ԋࢉࢠ • for(1 .. 10){ print $_; } •
ਖ਼͔ͳΓཉ͍͠
<?php macro { foreach ( T_LNUMBER·S .. T_LNUMBER·E as T_VARIABLE·INDEX
) { ···body } } >> { foreach ( range(T_LNUMBER·S , T_LNUMBER·E) as T_VARIABLE·INDEX ) { ···body } } foreach( 1 .. 10 as $_ ){ echo $_; }
foreach( 1 .. 10 as $_ ){ echo $_; }
// ↓ foreach ( range(1 , 10) as $_ ){ echo $_; }
12345678910
range ԋࢉࢠ ͦͷ̎ • ʮrange()Ͱforeachͱ͔μϝͰ͠ΐʯ ʮ͍ʯ • ʢੲͷPerlPHPಉ༷ʹڊେྻ͕Ͱ͖͍ͯͨ ʢΒ͍͠ʣʣ •
ʢࠓճ࣮͠ͳ͍͚Ͳ('a' .. 'z')ͳͲ
<?php macro { foreach ( T_LNUMBER·S .. T_LNUMBER·E as T_VARIABLE·INDEX
) { ···body } } >> { $__itelatorGenerator = itelatorGenerator(T_LNUMBER·S, T_LNUMBER·E); foreach ( $__itelatorGenerator as T_VARIABLE·INDEX ) { ···body } } function itelatorGenerator($init, $fin){ $num = $init; while(1){ if( $fin < $num ) { return $num++; } yield $num++; } } foreach( 1 .. 10 as $_ ){ echo $_; }
function itelatorGenerator($init, $fin){ $num = $init; while(1){ if( $fin <
$num ) { return $num++; } yield $num++; } } // ͜͜·Ͱڞ௨ foreach( 1 .. 10 as $_ ){ echo $_; } // ↓ $__itelatorGenerator·cfcd208495d565ef66e7dff9f98764da = itelatorGenerator(1, 10); foreach ( $__itelatorGenerator·cfcd208495d565ef66e7dff9f98764da as $_ ) { echo $_; }
12345678910
ਖ਼نදݱϚονͱஔ • Ϛον • if( "123" =~ /2/ ){ }
• ஔ • $abc =~ s/a/A/; • ݸਓతʹ݁ߏ͖ • ʮPerlه߸ଟਿʯ ʮperlopͰάάΕʯ
<?php macro { T_VARIABLE·A =~ /T_STRING·B/ } >> { preg_match(
'/'. ··stringify(T_STRING·B) .'/u', T_VARIABLE·A ) } if($abc =~ /a/){ echo 1; }
if($abc =~ /a/){ echo 1; } // ↓ if(preg_match( '/'.
'a' .'/u' ,$abc )){ echo 1; } # օ͞Μ͕͍͍͍ͨ͜ͱղΔ
<?php macro { T_VARIABLE·A =~ s/T_STRING·B/T_STRING·C/ } >> { T_VARIABLE·A
= preg_replace( '/'. ··stringify(T_STRING·B) .'/u', ··stringify(T_STRING·C) , T_VARIABLE·A ) } $abc = 'abc'; $abc =~ s/a/v/; echo $abc;
$abc =~ s/a/v/; // ↓ $abc = preg_replace( '/' .
'a' . '/u', 'v' , $abc );
༨ஊ:JS෩ਖ਼نදݱϚον • if($abc./a/){ ... } • ʮOOPͬΆ͍ʂʯ ʮݩʑΦϒδΣΫτࢦݴޠ͔ͩͧͬ͠Γ͠Ζʯ • ʮPHPͳΒ
−>Ͱʁʯ ʮͤͳʯ
<?php macro { if( T_VARIABLE·A./T_STRING·B/ ){ ···body } } >>
{ if( preg_match( '/'. ··stringify(T_STRING·B) .'/u' , T_VARIABLE·A ) ) { ···body } } $abc = 'abc'; if($abc./a/){ echo 1; }
if($abc./a/){ echo 1; } // ↓ if( preg_match( '/'. 'a'
.'/u' , $abc ) ){ echo 1; }
yadayada • ... • ʮޙͰ࣮͢Δ͔Β...ʯͱ͍͏࣌ͷʮ...ʯ • ౸ୡ͢Δͱɺྫ֎্͕͕Δ • //TODOΑΓΘ͔Γ͍͢…͔…͠Εͳ͍
<?php macro { ... } >> { throw new \Exception
('Unimplemented'); } if(1){ ... }
if(1){ ... } // ↓ if(1){ throw new \Exception ('Unimplemented');
}
block scope • {ʙ} • ϒϩοΫͷதͰείʔϓ͕ग़དྷΔ • ͋ΔҙຯҰ൪ϚΫϩͬΆ͍ • ΊͬͪΌ΄͍͠ʂʂʂ
<?php macro { { ···body } } >> { (
function( $arg ) { extract( $arg ); ···body } ) ( get_defined_vars() ) ; } $abc = 'abc'; { $abc = 'def'; echo $abc, PHP_EOL; } echo $abc, PHP_EOL;
{ $abc = 'def'; } // ↓ ( function( $arg·cfSNIPda
) { extract( $arg·cfSNIPda ); $abc = 'def'; } ) ( get_defined_vars() ) ;
def abc
use • ʮPHPͷuseͬͯͳΜͳͷɺಡΈࠐΜͰ͘Εͳ͍ͷ Α͘Θ͔Βͳ͍Μ͚ͩͲ…ʯʢ༑ਓͷJVMܥϓϩ άϥϚஊʣ ʮͨɺ͔ͨ͠ʹ…ʯ • useͰຊʹrequire_once͢Εղܾ
<?php macro { use ·ns()·type ; } >> { use
·type ; __require( ··stringify(·type) ) ; } function __require($class){ $array = explode('\\', ltrim($class, '\\')); $path = implode('/', $array); $filename = $path.'.php'; require_once($filename); } use \This\Is\Test;
use \This\Is\Test; // ↓ use \This\Is\Test ; __require( '\This\Is\Test' )
;
use strict; use warnings; • ͍ܰΤϥʔͰऴྃ͢Δ • ͍͋͠ॴͰܯࠂ͢Δ • ʢຊ͏ͪΐͬͱҙຯ͕͋Δʣ
• ʢηοτ͍ͯ͠ΔΤϥʔϋϯυϥ࡞๏ݸਓతझ ຯͰ͢ʣ
<?php macro { use strict; } >> { set_error_handler( function($errno,
$errstr, $errfile, $errline){ throw new \Error($errstr); } ); } macro { use warnings; } >> { error_reporting(-1); } use strict; use warnings; echo $a;
use strict; use warnings; // ↓ <?php set_error_handler( function( $errno·cfSNIPda,
$errstr·cfSNIPda, $errfile·cfSNIPda, $errline·cfSNIPda ){ throw new \Error($errstr·cfSNIPda); } ); error_reporting(-1);
·ͱΊ • yayͬͯ݁ߏศརͰʂʢʁʣ • ʮҰਓϓϩδΣΫτझຯͳΒͳʂʯ • ஔͱ͔Ͱͳ͍ͷͰɺ ʢͪΌΜͱॻ͚ʣ͔ͳΓյΕͮΒ͍ • ग़ྗ͞ΕΔͷɺຊͷPHPͳͷͰ҆৺ʢʁʣ
• ୯७ͳϧʔλʔఆٛͷDSLͳΒͭ͘Ε·ͨ͠
ຊʹΔਓͷίϝϯτ • yayɺυΩϡϝϯτ͕ςετίʔυ͔͠ͳ͍ͷͰௐ Δͷ͕େม • τʔΫϯछͷT_VARIABLE github/nikic/PHP-Parser • ύʔαʔͷ·ls github/marcioAlmada/yay/src/
parsers.php • ʮͱʹ͔࣮͘ͱςετίʔυΛݟΔΜͩʯ
• ·ͱ.ͱɾΛؒҧ͑ͯେม • middle dot • ʮ·(00B7)ͬͯɺීஈͲ͏ͬͯೖྗ͢ΔΜ…ʯ • ʮதࠇΛ·(00B7)ʹม͢Δpreprocessor͕ඞཁͰ ʯ
__END__
__halt_compiler();
ʮyapc8oji Ͱάάͬͯ͘ΕཔΉʯ τʔΫԠืͯ͘͠Εʂʂ̍
͓·͚ɿshort closure • ( )->{ }
<?php macro { (···condition) -> { ···body } } >>
{ function ( ···condition ) { ···body } } $a = ($v)->{ echo $v; }; $a('abc');
$a = ($v)->{ echo $v; }; // ↓ $a =
function ( $v ) { echo $v; };
Defined-Or, //= ԋࢉࢠ • $a //= 'default'; • ʢ//??ͱಉ͡ʣ •
ຊؾͰཉ͍͠ • 7.xͰདྷΔʢ͚ͲઌऔΓͱ͍͏͜ͱͰʣ => https://wiki.php.net/rfc/ null_coalesce_equal_operator
<?php macro { T_VARIABLE·A ??= } >> { T_VARIABLE·A =
T_VARIABLE·A ?? } $a ??= 123; echo $a;
$a ??= 123; echo $a; // ↓ $a = $a
?? 123; echo $a;
splitͱjoin • ʮͲ͏ͯ͠Explode/Implode͕׳Εͳ͍ʯ ʮ׳ΕΖʯ ʮͳͥจࣈΛ૿͢ํʹߦͬͨͷ͔ʯ ʮఘΊΖʯ • PHP7Ͱ͍ͭʹফ໓͠·ͨ͠Ͷ
<?php macro { split } >> { mb_split } macro
{ join } >> { implode } print_r( split(",", "a,b,c") ); print_r( join(",", ["a","b","c"]) ); # PHP7Ͱsplit()͕ͳ͘ͳͬͯ # mb_splitͷ͍ͬͯ͜ΔΜͩΑͳ͋…
split(",", "a,b,c"); join(",", ["a","b","c"]); // ↓ mb_split(",", "a,b,c"); implode(",", ["a","b","c"]);