Upgrade to Pro — share decks privately, control downloads, hide ads and more …

self in return type declaration

7b606c5039f083d13e2d2320ce6ddcfa?s=47 DQNEO
June 27, 2018

self in return type declaration

PHPの戻り値型宣言でselfを使ってみよう

7b606c5039f083d13e2d2320ce6ddcfa?s=128

DQNEO

June 27, 2018
Tweet

Transcript

  1. !%2/&0 Ͳ͖ΎͶ͓ BU.FSDBSJ QIQTUVEZ lTFMGzJO 3FUVSO5ZQF%FDMBSBUJPO

  2. (JUຊͷ؂म͠·ͨ͠

  3. 3FUVSOUZQFEFDMBSBUJPO ໭Γ஋ͷܕએݴ ࢖ͬͯ·͔͢ʁ

  4. function foo(): int { } ໭Γ஋ͷܕએݴͱ͸ʁ ͜Μͳ΍ͭ

  5. ྫɿࣗ෼Λฦ͢ϝιου class A { public function getMe(): A { return

    $this; } }
  6. ࣮͸TFMGͱॻ͚Δ class A { public function getMe(): self { return

    $this; } }
  7. ΂ΜΓʂʂ

  8. ҙ֎ͱ஌Βͳ͍ਓ ଟ͍ҹ৅

  9. ެࣜυΩϡϝϯτʹ͸ ͳ͔ͥTFMGʹ͍ͭͯ ݴٴͳ͠ IUUQQIQOFUNBOVBMFOGVODUJPOTSFUVSOJOH WBMVFTQIQGVODUJPOTSFUVSOJOHWBMVFTUZQFEFDMBSBUJPO

  10. 3'$ͷԼͷํʹখ͞ʙ͘ ॻ͍ͯ͋Δ IUUQTXJLJQIQOFUSGDSFUVSO@UZQFT

  11. ܧঝ͢ΔͱͲ͏ͳΔʁ class A { public function getMe(): self { return

    $this; } } class B extends A {} $b = new B(); $b->getMe(); // ?
  12. ਖ਼ղ#ͷΠϯελϯε͕ฦΔ class A { public function getMe(): self { return

    $this; } } class B extends A {} $b = new B(); $b->getMe(); // instance of B
  13. UIJT͸ɺ#ͷίϯςΩε τ͔Βݺ͹ΕΔͱ#ͷΠ ϯελϯεΛࢦ͢ͷͰɻ

  14. ࠨͷίʔυ͸ӈͷίʔυͱ౳Ձ class A { public function getMe(): A { return

    $this; } } class B extends A {} $b = new B(); $b->getMe(); // B class A { public function getMe(): self { return $this; } } class B extends A {} $b = new B(); $b->getMe(); // B
  15. ໭Γ஋ͷܕ͸ɺ ࣮ߦ࣌ʹSFUVSO͞Εͨࡍʹ JOTUBODFPG తͳ΋ͷͰ  ͰνΣοΫ͞ΕΔ

  16. ͜ͷ৔߹Ͳ͏ͳΔʁ class A { public function getMe(): self { return

    $this; } } class B extends A { public function getMe(): self { return $this; } }
  17. 'BUBMFSSPS
 ࣮ߦ࣌ίϯύΠϧΤϥʔ 1)1'BUBMFSSPS %FDMBSBUJPOPG#HFU.F #NVTUCFDPNQBUJCMFXJUI "HFU.F "

  18. ͜Ε͕μϝͳͷͱಉ͡
 γάωνϟͷෆҰக  class A { public function getMe(): A

    … } class B extends A { public function getMe(): B … }
  19. ͜͏͢Ε͹͓̺ class A { public function getMe(): self { return

    $this; } } class B extends A { public function getMe(): parent { return $this; } }
  20. ͜Ε͕0,ͳͷͱಉ͡ class A { public function getMe(): A … }

    class B extends A { public function getMe(): A … }
  21. ༨ஊɿ͜Ε͸কདྷͷ1)1Ͱ͸ ՄೳʹͳΔ͔΋͠Εͳ͍ɻ ໭Γ஋ͷڞม  class A { public function getMe():

    A } class B extends A { public function getMe(): B … } IUUQTXJLJQIQOFUSGDDPWBSJBOUSFUVSOTBOE DPOUSBWBSJBOUQBSBNFUFST
  22. ͪͳΈʹܕ໊ͷͱ͜Ζʹ TUBUJD͸ෆՄɻ class A { public function getMe(): static …

    }
  23. ·ͱΊ w ໭Γ஋ܕએݴͰTFMG QBSFOU ͕࢖͑Δ w ཚ༻͢ΔͱΘ͔Γʹ͘͘ͳΔ ͷͰదࡐదॴͰ w ʮJOTUBODFPGνΣοΫʯͩͱ

    ࢥ͑͹͓̺
  24. ༨ஊɿϝιουͷҾ਺Ͱ΋࢖͑Δ class A { public function setMe(self $self) { $this->self

    = $self; } } class B extends A { public function setMe(parent $self) { $this->self = $self; } }
  25. ͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ NN