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

極める!メソッドチェーン #phpstudy / fluent-interface

極める!メソッドチェーン #phpstudy / fluent-interface

2017/07/26 第116回 PHP勉強会の発表資料です。

Hiraku NAKANO

July 26, 2017
Tweet

More Decks by Hiraku NAKANO

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • த໺ ୓ (@Hiraku) • https://github.com/hirak • ϝϧΧϦͷαʔόʔαΠυΤϯδχΞ •

    Composerؔ࿈ͷൃදΛΑ͍ͯ͘͠·͢ • PHPΧϯϑΝϨϯεؔ੢2016 جௐߨԋ
  2. ϝιουνΣʔϯͱ͸ $talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ;

    ΦϒδΣΫτʹରͯ͠ɺ
 ϝιουΛଓ͚ͯॻ͚ΔΑ͏ʹ
 ͨ͠ύλʔϯ/APIσβΠϯͷ͜ͱ
  3. $talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ; $talk->setTitle('art

    of method chain'); $talk->setContent('no plan'); $talk->setLength('20min'); LoD͸कͬͨํ͕͍͍ͷʁ ϝιουͷ໭Γ஋ is Կʁ ͜ΕͳΒ໭Γ஋Λؾʹ͢Δ·Ͱ΋ͳ͍
  4. ϝιουνΣʔϯͱ͸ $talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ;

    ΦϒδΣΫτʹରͯ͠ɺ
 ϝιουΛଓ͚ͯॻ͚ΔΑ͏ʹ
 ͨ͠ύλʔϯ/APIσβΠϯͷ͜ͱ
  5. 1) return $this class Chainable { function setA($val) { $this->a

    = $val; return $this; } function setB($val) { $this->b = $val; return $this; } } (new Chainable) ->setA('a') ->setB('b'); ࿈ଓͰsetͰ͖ΔΑ͏ʹͳͬͨΑʂ
  6. immutable
 ʹର͢Δݸਓతཧղ • ໭Γ஋Λड͚औΒͳ͍ͱ݁Ռ͕ফ͑ͯ͠·͏ • $obj->setFoo($foo);
 $obj->setBaa($baa);
 ͯ͠΋ҙຯ͕ͳ͍ • $obj

    = $obj->setFoo($foo);
 $obj = $obj->setBaa($baa); • ه๏͕໘౗ͳͷͰɺϝιουνΣʔϯʹͨ͘͠ͳΔ(Θ͔Δ)
  7. <?php abstract class Context { protected $parent = null; function

    __construct(Context $parent=null) { $this->parent = $parent; } abstract function if(bool $bool): Context; abstract function say(string $str): Context; function endif(): Context { return $this->parent; } } class TrueContext extends Context { function if(bool $bool): Context { if ($bool) { return new self($this); } else { return new FalseContext($this); } } function say(string $str): Context { echo $str,PHP_EOL; return $this; } } class FalseContext extends Context { function if(bool $bool): Context { return new self($this); } function say(string $str): Context { return $this; } } function dsl(): TrueContext { return new TrueContext; } ͍͍ͩͨ ͜Ε͙Β͍ Ͱ࡞ΕΔ ϝιουνΣʔϯDSLͰϒϩοΫΛ࡞ΔςΫχοΫ: Architect Note http://blog.tojiru.net/article/272060766.html
  8. SELECTจͷ෮श ͍͍ͩͨҎԼͷॱͰॲཧ͞ΕΔ FROM → JOIN → WHERE → GROUP BY

    → HAVING → PROJECT → ORDER BY → LIMIT → UNION ࢦఆॱΛ͜ΕͰݻఆͰ͖ΔΑ͏ʹAPIઃܭ