Save 37% off PRO during our Black Friday Sale! »

mixin実装の現状と課題

 mixin実装の現状と課題

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

8a7784885ef08ecd5e96d9b93e610c65?s=128

Ryusuke SEKIYAMA

March 15, 2014
Tweet

Transcript

  1. mixin実装�の現状と課題 @rsky 2014-03-15 第五回闇PHP勉強会

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

    <?php trait FooFighter { function foo() { echo “FOO\n”; } } ! class A { use FooFighter; } ! class B { use FooFighter; }
  3. Ұํɺ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
  4. lΠϯελϯεԽ࣌ʹ΋ϛοΫεΠϯͰ͖Δz

  5. 1)1Ͱ΋͜Ε΍Γ͍ͨʂ

  6. Ͱ΋೉͍͠ΜͰ͠ΐ͏

  7. Ͱ͖ͨʂ http://blog.r-sky.jp/post/38388457154/qiq-reborn https://github.com/rsky/php-src/tree/scala-like-mixin

  8. Ͱ͖ͨ͜ͱ ΠϯελϯεԽ࣌ʹ 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();
  9. Ͱ͖͍ͯͳ͍͜ͱ Precedenceʢ༏ઌॱҐ ΍ผ໊ͷఆٛʣ <?php trait FooFighter { function foo() {

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

    ܗࣜͷͨΊɺτϨΠτͱί ϯετϥΫλҾ਺͕ฆΒΘ͍͠ɻ
 →࠷৽൛Ͱ͸τϨΠτ͕1ͭͰ΋ׅހඞਢʹͨ͠ <?php $ab = new A use B ($arg1, $arg2); $abc = new A use (A, B) ($arg1, $arg2);
  11. ͜͏͍ͨ͠ʂ useεςʔτϝϯτΛίϯετϥΫλҾ਺ͷޙʹɻ Precedence΋ॻ͚Δɻ Precedence಺Ͱ΋ม਺ͰΫϥεղܾͰ͖Δɻ <?php $ab = new A ($arg1,

    $arg2) use B; $abc = new A ($arg1, $arg2) use ($b, $c) { $b::foo insteadof $a; $c::foo as bar; };
  12. ͜Ε΋͋Γʁ τϨΠτͷϦετΛׅހͰׅ͍ͬͯΔͷ͸ߏจ্ ͷ༏ઌॱҐͷͨΊͳͷͰɺScalaͷΑ͏ʹෳ਺ͷ useεςʔτϝϯτΛྻڍ͢Δख΋͋Δɻ <?php $abc = new A ($arg1,

    $arg2) use B use C;
  13. ٕज़త՝୊ yaccతʹໃ६͕ͳ͍จ๏ఆٛ·Ͱ͸
 ʢखݩͷόʔδϣϯͰ͸ʣͰ͖͍ͯΔɻ Ҿ਺Ϧετ͕࢝·ΔΑΓલʹΠϯελϯεԽ͢Δ Ϋϥε͕֬ఆ͍ͯ͠ͳ͍ͱ͍͚ͳ੍͍ݶ͕͋Δɻ <?php $abc = new A

    ($arg1, $arg2) use (B, C);
  14. ղܾҊA ຊ࣭తղܾʁ ਖ਼௚ɺ೉͍͠ Zend EngineΛ৽͍͠จ๏ͷͨΊʹΰϦΰϦॻ͖׵͑Δ

  15. ղܾҊB new A($arg) use(B, C) ↓ new A use (B,

    C) ($arg) ϓϦϓϩηοαΛ͔·ͯ͠ਓʹ΍͍͞͠ίʔυΛ! ػցʹ΍͍͞͠ίʔυʹ੔ܗ͔ͯ͠ΒίϯύΠϧ ݸਓతʹ͸ͬͪ͜ͷํ͕޷Έ PHP: Hypertext Preprocessor
  16. ղܾҊC (ABં஭?) ίϯύΠϥΛ͍ͬͯ͘͡op_arrayΛฒͼସ͑Δ ίʔυ্Ͱ͸͜͏ new A($arg) use(B, C) ίϯύΠϧ͞ΕͨόΠτίʔυͰ͸͜Εͱ౳Ձ new

    A use(B, C)($arg)
  17. ୭͔͓ئ͍͠·͢ʂ