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

self in return type declaration

DQNEO
June 27, 2018

self in return type declaration

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

DQNEO

June 27, 2018
Tweet

More Decks by DQNEO

Other Decks in Technology

Transcript

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

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

    $this; } } class B extends A {} $b = new B(); $b->getMe(); // instance of B
  3. ࠨͷίʔυ͸ӈͷίʔυͱ౳Ձ 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
  4. ͜ͷ৔߹Ͳ͏ͳΔʁ class A { public function getMe(): self { return

    $this; } } class B extends A { public function getMe(): self { return $this; } }
  5. ͜Ε͕μϝͳͷͱಉ͡
 γάωνϟͷෆҰக  class A { public function getMe(): A

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

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

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

    A } class B extends A { public function getMe(): B … } IUUQTXJLJQIQOFUSGDDPWBSJBOUSFUVSOTBOE DPOUSBWBSJBOUQBSBNFUFST
  9. ༨ஊɿϝιουͷҾ਺Ͱ΋࢖͑Δ class A { public function setMe(self $self) { $this->self

    = $self; } } class B extends A { public function setMe(parent $self) { $this->self = $self; } }