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
340
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
380
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
0
670
入門Go言語仕様 / Go Specification Untyped Constants
dqneo
0
920
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
8
3.9k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.4k
もっと気軽にOSSに Pull Requestを出そう!/ Let's make a PR to OSS more easily
dqneo
6
7.6k
Goコンパイラをゼロから作ってセルフホスト達成するまで / How I wrote a self hosted Go compiler from scratch
dqneo
14
13k
コンパイラをつくってみよう / How to make a compiler
dqneo
9
10k
コンパイラ作りの魅力を語る / Making compilers is fun
dqneo
9
8.1k
Other Decks in Programming
See All in Programming
自動テスト実行結果の目的を整理する / Organizing objectives of automated test results
twada
PRO
10
2.1k
Migrating to Signals: A Practical Workshop
manfredsteyer
PRO
0
280
マイ隙間家具OSSたちのご紹介
karupanerura
2
160
ONE WEDGE_Company_Information
1wedge
0
180
RubyVM を PHP で実装する 〜Hello World を出力するまで〜
memory1994
PRO
1
490
フロントエンドパフォーマンス 入門
shouta2
7
1.5k
生成 AI の中身を覗いてみよう〜基礎から医療現場での応用まで〜
soh9834
2
770
ここ1~2年くらいで 使えるようになった(主要ブラウザーの最新版 がすべて対応した ) ウェブの新機能について ランダムに喋る!
myzkyy
9
6.5k
Enhancing Applications with Accessibility API
kishikawakatsumi
3
980
Open Source Swift Workshop - Foundation and first party libraries
ikesyo
0
270
コミュニティに参加したことで起きた変化
ohmori_yusuke
3
130
「コンパイル時のユニットテスト」導入するとユニットテストを 書かなくてよくなるのか?
tomohisa
9
2.2k
Featured
See All Featured
Design by the Numbers
sachag
274
18k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
113
18k
Code Review Best Practice
trishagee
54
15k
Robots, Beer and Maslow
schacon
PRO
154
7.9k
Statistics for Hackers
jakevdp
789
220k
Automating Front-end Workflow
addyosmani
1353
200k
The Invisible Customer
myddelton
114
12k
What's new in Ruby 2.0
geeforr
335
31k
Keith and Marios Guide to Fast Websites
keithpitt
407
22k
Web development in the modern age
philhawksworth
201
10k
Rails Girls Zürich Keynote
gr2m
91
13k
No one is an island. Learnings from fostering a developers community.
thoeni
14
2k
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