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
370
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
490
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
6
1.6k
入門Go言語仕様 / Go Specification Untyped Constants
dqneo
1
1.2k
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
9
4.9k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.6k
もっと気軽に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
データベースエンジニアの仕事を楽にする。PgAssistantの紹介
nnaka2992
9
4.4k
SQL Server ベクトル検索
odashinsuke
0
150
アーキテクトと美学 / Architecture and Aesthetics
nrslib
12
3.2k
S3静的ホスティング+Next.js静的エクスポート で格安webアプリ構築
iharuoru
0
210
Signal-Based Data FetchingWith the New httpResource
manfredsteyer
PRO
0
130
自分のために作ったアプリが、グローバルに使われるまで / Indie App Development Lunch LT
pixyzehn
1
140
List とは何か? / PHPerKaigi 2025
meihei3
0
590
The Weight of Data: Rethinking Cloud-Native Systems for the Age of AI
hollycummins
0
230
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
200
DataStoreをテストする
mkeeda
0
260
AI時代のプログラミング教育 / programming education in ai era
kishida
23
21k
リアクティブシステムの変遷から理解するalien-signals / Learning alien-signals from the evolution of reactive systems
yamanoku
2
1.2k
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
8
720
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
31
4.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
12
630
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
GraphQLとの向き合い方2022年版
quramy
45
14k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Agile that works and the tools we love
rasmusluckow
328
21k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
We Have a Design System, Now What?
morganepeng
51
7.5k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
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