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
PHP: null, default null, nullable and void
Search
DQNEO
July 25, 2018
Programming
0
360
PHP: null, default null, nullable and void
PHPのnull, = null, nullable, voidについておさらいしてみよう
DQNEO
July 25, 2018
Tweet
Share
More Decks by DQNEO
See All by DQNEO
英和辞書付きGo言語仕様書 / Word Wise Go Spec
dqneo
1
480
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
6
1.5k
入門Go言語仕様 / Go Specification Untyped Constants
dqneo
1
1.2k
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
9
4.8k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.5k
もっと気軽にOSSに Pull Requestを出そう!/ Let's make a PR to OSS more easily
dqneo
6
8.2k
Goコンパイラをゼロから作ってセルフホスト達成するまで / How I wrote a self hosted Go compiler from scratch
dqneo
15
14k
コンパイラをつくってみよう / How to make a compiler
dqneo
9
11k
コンパイラ作りの魅力を語る / Making compilers is fun
dqneo
10
8.4k
Other Decks in Programming
See All in Programming
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
260
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
140
未経験でSRE、はじめました! 組織を支える役割と軌跡
curekoshimizu
1
120
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
4
920
GoとPHPのインターフェイスの違い
shimabox
2
200
Conform を推す - Advocating for Conform
mizoguchicoji
3
710
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
250
Honoをフロントエンドで使う 3つのやり方
yusukebe
7
3.5k
負債になりにくいCSSをデザイナとつくるには?
fsubal
10
2.5k
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
920
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
280
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
220
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
10
1.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
Designing Experiences People Love
moore
140
23k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
It's Worth the Effort
3n
184
28k
Typedesign – Prime Four
hannesfritz
40
2.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Transcript
!%2/&0 Ͳ͖ΎͶ͓ BU.FSDBSJ *OD QIQTUVEZ OVMM OVMM OVMMBCMF WPJE ʹ͍͓ͭͯ͞Β͍͠Α͏
OVMMͱԿ͔
σϑΥϧτOVMMͱ
OVMMBCMFͱ
WPJEͱ
ࠓ ʮͳ͍ʯͱԿ͔ʹ͍ͭͯ ߟ͑ͯΈΑ͏
OVMMͱԿ͔ • null型のnullという値 (null型にはnullという1種類の値しかな い)
͜ΕOVMM $x = null var_dump($x);
͜ΕOVMM function f() { } var_dump(f()); 関数が何も返さなかった場合、戻り値はnullとなる
σϑΥϧτOVMMͱ function f(int $a, int $b = null) { return
[$a, $b]; } f(1); // [1, null] 引数を渡さなかったときのデフォルト値
ͱࢥ͏͡Όͳ͍Ͱ͔͢ʁ ͦΕ͚ͩ͡Όͳ͍ΜͰ͢
function f(int $a = null, int $b) { } 引数を渡さなかったときのデフォルト値?
function f(int $a = null, int $b) { return [$a,
$b]; } f(1,2); => [1,2] f(1); => Fatal error f(); => Fatal error 引数を渡さなかったときのデフォルト値? 第1引数を渡さないってそれ無理では?
function f(int $a = null, int $b =null, in t
$c) { } f(null, null, 1); => [null, null, 1] もはや「デフォルト値」じゃなくない? だって引数渡してるもん
ͦ͏ɺ ඞਢҾΑΓࠨʹ͋ΔOVMMશͯ ʮσϑΥϧτʯ ͷׂΛࣦ͍ͬͯΔ
OVMM ͏ͻͱͭผͷҙຯ͕͋Δɻ ʮOVMMΛड͚ͱΓՄೳʯ
ʮOVMMΛड͚ͱΓՄೳʯ ͭ·ΓOVMMBCMF
function f(int $a = null, int $b) { } function
f(?int $a, int $b) { } この場合は= nullじゃなくてnullableを使おう 可読性向上!
ʮσϑΥϧτҾʯͷ߹ OVMMBCMFએݴͰ͖Δ function f(int $a, int $b = null) {
} function f(int $a, ?int $b = null) { }
ͪͳΈʹ ʮσϑΥϧτOVMMʯͷ ׂΛ࣋ͨͳ͍ function f(int $a, ?int $b) { }
f(1); => Fatal error
ΓͷOVMMBCMFએݴɺ ͳͩ͠ͱΤϥʔ function f() ?int { return; } f(); =>
Fatal error
ΓͷOVMMBCMFએݴɺ ໌ࣔతʹOVMMΛฦ͢ඞཁ͋Γ function f() ?int { return null; } f();
=> null
WPJEͱ
1)1͔Βೖͬͨ৽ߏจ function f() :void { } function f() :void {
return; } 関数が値を返さないことを宣言できる
ݺͼग़͠ଆࢹͩͱOVMM function f() :void { } var_dump(f()); // null void型関数を実行するとnullが得られる
͔͠͠OVMMΛSFUVSO͢ΔͱΤϥʔ function f() :void { return null; } var_dump(f()); //Fatal
error: A void function must not return a value 「returnの右に式を書いてはいけない」くらいの意味
w OVMM w σϑΥϧτOVMM w OVMMBCMF w WPJE Ҏ্ɺ ʹ͍͓ͭͯ͞Β͍͠·ͨ͠
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ NN