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

Trait とは? その使い道を考えてみる

Trait とは? その使い道を考えてみる

2012/09 の社内 LT で発表した資料

B6b32d5d2a05b7cebaea86bdf3e95077?s=128

Takuya Fujimura

September 07, 2012
Tweet

Transcript

  1. Trait ͱ͸? ͦͷ࢖͍ಓΛߟ͑ͯΈΔ ౻ଜ ୓໵ @tlync 12೥8݄9೔໦༵೔

  2. ࣗݾ঺հ 12೥8݄9೔໦༵೔

  3. ࣗݾ঺հ • ౻ଜ ୓໵@tlync • ΞϓϦέʔγϣϯ։ൃୈೋ • ࠷ۙͷؔ৺ࣄ • Scala,

    Agile(Project Mgt.) • PHP ͸ੜ͖Δҝʹ΍͍ͬͯ·͢ 12೥8݄9೔໦༵೔
  4. ※ ͪͳΈʹ࢒೦ͳ͕Β? PHP ͷ Trait ͷ͓࿩͠Ͱ͢ 12೥8݄9೔໦༵೔

  5. Trait Λ஌ͬͯΔਓ? 12೥8݄9೔໦༵೔

  6. Trait Λઆ໌Ͱ͖Δਓ? 12೥8݄9೔໦༵೔

  7. USBJUUSÉJUcUSÉJ ໊ɹࢺ⽣ܗࣜత⽤ ʢੑ֨श׳ͷʣಛ௃ ಛ৭ ಛ࣭ BENJSBCMFUSBJUTMJLFIPOFTUZBOEDPVSBHFʛ੣࣮͞΍༐ؾͱ ͍ͬͨ͢͹Β͍͠ಛ࣭ "NFSJDBOUSBJUTʛถࠃࠃຽੑ ⽣ر༻ޠ⽤ʢϖϯԖචͳͲͷʣҰච ච࢖͍ʀʢੑ࣭ͳͲ

    ͷʣؾຯ ؾ഑⽣PG⽤ BUSBJUPGTBEOFTTʛҰ·ͭͷ൵ѩ 12೥8݄9೔໦༵೔
  8. ಛ௃ ಛ৭ ಛ࣭ 12೥8݄9೔໦༵೔

  9. Trait ͱ͸? • ϝιουͱϓϩύςΟͷू߹ • ୯Ұܧঝͷ੍໿Λܰݮ͢Δ • ܧঝؔ܎ͱ͸ؔ܎ͳ͘ɺڞ௨ͷಛੑɺ ৼ෣͍Λ࠶ར༻(ਫฏల։)Ͱ͖Δ •

    ͦΕࣗମͰ͸ΠϯελϯεԽͰ͖ͳ͍ 12೥8݄9೔໦༵೔
  10. ܧঝؔ܎ͱؔ܎ͳ͘ ࣮૷ Λ ࠶ར༻ Ͱ͖Δ࢓૊Έ 12೥8݄9೔໦༵೔

  11. PHP 5.4 <= 12೥8݄9೔໦༵೔

  12. ଞݴޠͰͷྨࣅͷػߏ • Scala … trait • Perl 6 … role

    • Ruby … Mix-in ※ಉ༷ͷ֓೦͕ͩ࢓༷͸ඍົʹҟͳΔɻ ܕ৘ใΛ͔࣋ͭ(is_aΛύε͢Δ͔)ͱ͔ɻ 12೥8݄9೔໦༵೔
  13. ؆୯ͳαϯϓϧ 12೥8݄9೔໦༵೔

  14. trait Engine { public function startEngine() { echo 'BRRRM!'; }

    } class Car { use Engine; } (new Car)->startEngine(); // BRRRM! 12೥8݄9೔໦༵೔
  15. ͜Ε͸ܧঝͰ΋ Ͱ͖ͳ͘͸ͳ͍ 12೥8݄9೔໦༵೔

  16. Ͱ͸ɺ͜Ε͸? 12೥8݄9೔໦༵೔

  17. class Vehicle { protected function startEngine() { echo '⚡'; }

    } class Plane extends Vehicle { public function fly() { echo '✈'; } } class Boat extends Vehicle { public function sail() { echo '⚓'; } } 12೥8݄9೔໦༵೔
  18. class SeaPlane extends ??? { // Ϙʔτͯ͠૸Β͍ͤͨ͠ɺඈߦػ ͱͯ͠ඈ͹͍ͨ͠ɻͲ͏͢Ε͹… } 12೥8݄9೔໦༵೔

  19. startEngine() Vehicle sail() Boat fly() Plane sail() fly() SeaPlane ???

    12೥8݄9೔໦༵೔
  20. ଟॏܧঝ͸Ͱ͖ͳ͍ 12೥8݄9೔໦༵೔

  21. ҕৡ(Composition) ? 12೥8݄9೔໦༵೔

  22. interface Plane { public function fly(); } interface Boat {

    public function sail(); } 12೥8݄9೔໦༵೔
  23. class SimplePlane extends Vehicle implements Plane { public function fly()

    { echo '✈'; } } class SimpleBoat extends Vehicle implements Boat { public function sail() { echo '⚓'; } } 12೥8݄9೔໦༵೔
  24. class SeaPlane extends Vehicle implements Plane, Boat { private $boat;

    private $plane; public function __constract() { $this->boat = new SimpleBoat(); $this->plane = new SimplePlane(); } public function sail() { return $this->boat->sail(); } public function fly() { return $this->plane->fly(); } } $seaPlane = new SeaPlane(); $seaPlane->sail(); // ! $seaPlane->fly(); // ✈ 12೥8݄9೔໦༵೔
  25. ܗࣜతͳهड़͕ଟ͍ ΍΍໘౗ 12೥8݄9೔໦༵೔

  26. Trait Ͱ ࣮૷ͷڞ༗Λͯ͠ΈΔ 12೥8݄9೔໦༵೔

  27. // ໋໊͸ͪΐͬͱඍົ trait FlyEngine { public function fly() { echo

    '✈'; } } trait FloatEngine { public function sail() { echo '⚓'; } } class SeaPlane implements Plane, Boat // ܕͷܧঝ { use FloatEngine, FlyEngine; // ࣮૷ͷܧঝ } $seaPlane = new SeaPlane(); $seaPlane->sail(); // ! $seaPlane->fly(); // ✈ 12೥8݄9೔໦༵೔
  28. هड़͸͖ͬ͢Γ ࣮૷ͷڞ༗͕Ͱ͖ͨ 12೥8݄9೔໦༵೔

  29. startEngine() Vehicle sail() Boat fly() Plane sail() fly() SeaPlane sail()

    BoatEngine fly() FlyEngine sail() BoatEngine fly() FlyEngine 12೥8݄9೔໦༵೔
  30. ܕͷอূ … Interface ࣮૷ͷ࠶ར༻ … trait ※ ஫: Scala, Ruby

    ͳͲͰ͸·ͨͪΐͬͱҧ͏Α! 12೥8݄9೔໦༵೔
  31. Trait ͸ ܧঝπϦʔʹؔ܎ͳ͘ ࣮૷ͷ࠶ར༻Λߦ͏͚ͩ 12೥8݄9೔໦༵೔

  32. Ͱɺ࣮ࡍͲ͏࢖͏ͱศརͳͷ͔? 12೥8݄9೔໦༵೔

  33. 1. ந৅ΫϥεΛ࡞Δલʹ Trait ͷݕ౼ 12೥8݄9೔໦༵೔

  34. ந৅Ϋϥεͷओͳཧ༝͸ ίʔυͷ࠶ར༻(ଟ෼) 12೥8݄9೔໦༵೔

  35. Trait ͕ར༻Ͱ͖ͳ͍͔ݕ౼͢Δ 12೥8݄9೔໦༵೔

  36. ※Trait த2පʹ͸஫ҙ͠·͠ΐ͏ 12೥8݄9೔໦༵೔

  37. 2. Entity ͷৼ෣͍ͷڞ༗ 12೥8݄9೔໦༵೔

  38. Entity ͷৼ෣͍ • λΠϜελϯϓ؅ཧ • มߋͷτϨʔε • ιϑτσϦʔτՄೳ͔ • ͳͲ…

    12೥8݄9೔໦༵೔
  39. trait Timestampable { private $createdAt; private $updatedAt; ... getter, setter

    /** * Updates createdAt and updatedAt timestamps. */ public function updateTimestamps() { if (null === $this->createdAt) { $this->createdAt = new \DateTime('now'); } $this->updatedAt = new \DateTime('now'); } } 12೥8݄9೔໦༵೔
  40. /** * ঎඼ΤϯςΟςΟ */ class Product { use Timestampable, //

    λΠϜελϯϓΛࣗಈͰ SoftDeletable, // ιϑτσϦʔτՄೳʹ Activatable; // ׆ੑɺඇ׆ੑΛՄೳʹ private $name; // ຊ࣭తͳϩδοΫ } 12೥8݄9೔໦༵೔
  41. 3 12೥8݄9೔໦༵೔

  42. ࣌ؒͳ͔ͬͨ ʘ(^o^)ʗ 12೥8݄9೔໦༵೔

  43. ͍͍ΞΠσΟΞ͋ͬͨΒ ڭ͍͑ͯͩ͘͞ 12೥8݄9೔໦༵೔

  44. ·ͱΊ 12೥8݄9೔໦༵೔

  45. Trait Λ஌ΓɺPHP 5.4 ࣌୅ͷ ৽ͨͳઃܭΛ୳Ζ͏ 12೥8݄9೔໦༵೔

  46. ͦΕ͔ 12೥8݄9೔໦༵೔

  47. ͜ͷձ͔ࣾΒ PHP Λ๾໓͠Α͏ 12೥8݄9೔໦༵೔

  48. ͓ΘΓ 12೥8݄9೔໦༵೔