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
More Context, Better Code. 既存コードやOAS等をコンテキストとしてLLMに与える事で、よりよいコード生成を行う話
uzulla
1
98
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
3
170
セッションのトークセッション / Traps for PHP session features in growing web apps
uzulla
2
110
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
5
1.9k
例外を投げるのをやめてみないか? あるいは受け入れてみないか? - How to use exceptions other than throwing
uzulla
4
890
PHPerが ISUCONでやるべき事
uzulla
1
820
開発生産性は上がらない - N Ways to Reduce Development Productivity
uzulla
1
220
test is not a job
uzulla
1
480
あなたのPHPアプリ、ログはでてますか?あるいはログをだしてますか? / Are you writing a log? Or just out a log?
uzulla
17
7.4k
Other Decks in Programming
See All in Programming
subpath importsで始めるモック生活
10tera
0
230
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
200
Contemporary Test Cases
maaretp
0
120
CSC509 Lecture 09
javiergs
PRO
0
140
EventSourcingの理想と現実
wenas
6
2.3k
受け取る人から提供する人になるということ
little_rubyist
0
220
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
200
Tauriでネイティブアプリを作りたい
tsucchinoko
0
360
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
130
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
440
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
550
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
170
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Git: the NoSQL Database
bkeepers
PRO
427
64k
A better future with KSS
kneath
238
17k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
We Have a Design System, Now What?
morganepeng
50
7.2k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
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"]);