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

mixin実装の現状と課題

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 mixin実装の現状と課題

第五回闇PHP勉強会のミックスインについてLTしたスライドです

Avatar for Ryusuke SEKIYAMA

Ryusuke SEKIYAMA

March 15, 2014

More Decks by Ryusuke SEKIYAMA

Other Decks in Programming

Transcript

  1. PHP (>= 5.4) ͰͷTraits TraitͰܕʢΫϥεͷܧ ঝؔ܎ʣʹӨڹΛ༩͑ ͣʹػೳΛ࠶ར༻Ͱ͖ ΔΑ͏ʹͳͬͨɻ Ϋϥεએݴ࣌ʹ੩తʹ هड़͢Δɻ

    <?php trait FooFighter { function foo() { echo “FOO\n”; } } ! class A { use FooFighter; } ! class B { use FooFighter; }
  2. ҰํɺScalaͰ͸ ଞͷΫϥεΛܧঝ͠ͳ͍ ͱ͖͸extendsͰϛο ΫεΠϯɻ ଞͷΫϥεΛܧঝ͢Δͱ ͖͸withͰϛοΫεΠ ϯɻ ΠϯελϯεԽ࣌ʹ΋ϛο ΫεΠϯͰ͖Δɻ trait

    FooFighter { def foo = println(“FOO”) } ! class A extends FooFighter ! class B extends C with FooFighter ! val c = new C() with FooFighter
  3. Ͱ͖ͨ͜ͱ ΠϯελϯεԽ࣌ʹ use Traitͯ͠Ϋϥε Λੜ੒͢Δߏจɻ Ϋϥε໊ͷ୅ΘΓʹม ਺΋ར༻Մೳɻ
 →factoryύλʔϯ޲͖ <?php trait

    FooFighter { function foo() { echo “FOO\n”; } } ! class A {} ! (new A use FooFighter)- >foo(); ! $x = ‘FooFighter’; (new A use $x)->foo();
  4. Ͱ͖͍ͯͳ͍͜ͱ Precedenceʢ༏ઌॱҐ ΍ผ໊ͷఆٛʣ <?php trait FooFighter { function foo() {

    echo “FOO\n”; } } ! class A {} ! (new A use FooFighter { FooFighter::foo as bar; })->bar();
  5. Πέͯͳ͍͜ͱ new Class use (Trait1, Trait2, …) ($arg1, $arg2, …)

    ܗࣜͷͨΊɺτϨΠτͱί ϯετϥΫλҾ਺͕ฆΒΘ͍͠ɻ
 →࠷৽൛Ͱ͸τϨΠτ͕1ͭͰ΋ׅހඞਢʹͨ͠ <?php $ab = new A use B ($arg1, $arg2); $abc = new A use (A, B) ($arg1, $arg2);
  6. ղܾҊB new A($arg) use(B, C) ↓ new A use (B,

    C) ($arg) ϓϦϓϩηοαΛ͔·ͯ͠ਓʹ΍͍͞͠ίʔυΛ! ػցʹ΍͍͞͠ίʔυʹ੔ܗ͔ͯ͠ΒίϯύΠϧ ݸਓతʹ͸ͬͪ͜ͷํ͕޷Έ PHP: Hypertext Preprocessor