Download - Trait とは? その使い道を考えてみる
![Page 1: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/1.jpg)
Trait とは?その使い道を考えてみる
藤村 拓也 @tlync
12年8月9日木曜日
![Page 2: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/2.jpg)
自己紹介
12年8月9日木曜日
![Page 3: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/3.jpg)
自己紹介
• 藤村 拓也@tlync
• アプリケーション開発第二• 最近の関心事
• Scala, Agile(Project Mgt.)
• PHP は生きる為にやっています
12年8月9日木曜日
![Page 4: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/4.jpg)
※ ちなみに残念ながら? PHP の Trait のお話しです
12年8月9日木曜日
![Page 5: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/5.jpg)
Trait を知ってる人?
12年8月9日木曜日
![Page 6: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/6.jpg)
Trait を説明できる人?
12年8月9日木曜日
![Page 7: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/7.jpg)
trait /tréit | tréi/
名 詞⦅形式的⦆1 (性格習慣の)特徴, 特色, 特質admirable traits like honesty and courage|誠実さや勇気といったすばらしい特質American traits|米国国民性.2⦅希用語⦆(ペン鉛筆などの)一筆, 筆使い;(性質などの)気味, 気配⦅of ...⦆a trait of sadness|一まつの悲哀.
12年8月9日木曜日
![Page 8: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/8.jpg)
特徴,特色,特質
12年8月9日木曜日
![Page 9: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/9.jpg)
Trait とは?
• メソッドとプロパティの集合• 単一継承の制約を軽減する
• 継承関係とは関係なく、共通の特性、振舞いを再利用(水平展開)できる
• それ自体ではインスタンス化できない
12年8月9日木曜日
![Page 10: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/10.jpg)
継承関係と関係なく実装 を 再利用 できる仕組み
12年8月9日木曜日
![Page 11: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/11.jpg)
PHP 5.4 <=
12年8月9日木曜日
![Page 12: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/12.jpg)
他言語での類似の機構• Scala … trait
• Perl 6 … role
• Ruby … Mix-in
※同様の概念だが仕様は微妙に異なる。型情報を持つか(is_aをパスするか)とか。
12年8月9日木曜日
![Page 13: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/13.jpg)
簡単なサンプル
12年8月9日木曜日
![Page 14: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/14.jpg)
trait Engine{ public function startEngine() { echo 'BRRRM!'; }}
class Car{ use Engine;}
(new Car)->startEngine(); // BRRRM!
12年8月9日木曜日
![Page 15: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/15.jpg)
これは継承でもできなくはない
12年8月9日木曜日
![Page 16: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/16.jpg)
では、これは?
12年8月9日木曜日
![Page 17: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/17.jpg)
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日木曜日
![Page 18: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/18.jpg)
class SeaPlane extends ???{ // ボートして走らせたいし、飛行機として飛ばしたい。どうすれば…}
12年8月9日木曜日
![Page 19: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/19.jpg)
startEngine()
Vehicle
sail()
Boat
fly()
Plane
sail()fly()
SeaPlane
???
12年8月9日木曜日
![Page 20: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/20.jpg)
多重継承はできない
12年8月9日木曜日
![Page 21: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/21.jpg)
委譲(Composition) ?
12年8月9日木曜日
![Page 22: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/22.jpg)
interface Plane{ public function fly();}
interface Boat{ public function sail();}
12年8月9日木曜日
![Page 23: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/23.jpg)
class SimplePlane extends Vehicle implements Plane{ public function fly() { echo '✈'; }}
class SimpleBoat extends Vehicle implements Boat{ public function sail() { echo '⚓'; }}
12年8月9日木曜日
![Page 24: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/24.jpg)
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日木曜日
![Page 25: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/25.jpg)
形式的な記述が多いやや面倒
12年8月9日木曜日
![Page 26: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/26.jpg)
Trait で実装の共有をしてみる
12年8月9日木曜日
![Page 27: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/27.jpg)
// 命名はちょっと微妙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日木曜日
![Page 28: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/28.jpg)
記述はすっきり実装の共有ができた
12年8月9日木曜日
![Page 29: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/29.jpg)
startEngine()
Vehicle
sail()
Boat
fly()
Plane
sail()fly()
SeaPlane
sail()
BoatEngine
fly()
FlyEngine
sail()
BoatEngine
fly()
FlyEngine
12年8月9日木曜日
![Page 30: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/30.jpg)
型の保証 … Interface実装の再利用 … trait
※ 注: Scala, Ruby などではまたちょっと違うよ!
12年8月9日木曜日
![Page 31: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/31.jpg)
Trait は継承ツリーに関係なく実装の再利用を行うだけ
12年8月9日木曜日
![Page 32: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/32.jpg)
で、実際どう使うと便利なのか?
12年8月9日木曜日
![Page 33: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/33.jpg)
1. 抽象クラスを作る前に Trait の検討
12年8月9日木曜日
![Page 34: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/34.jpg)
抽象クラスの主な理由はコードの再利用(多分)
12年8月9日木曜日
![Page 35: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/35.jpg)
Trait が利用できないか検討する
12年8月9日木曜日
![Page 36: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/36.jpg)
※Trait 中2病には注意しましょう
12年8月9日木曜日
![Page 37: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/37.jpg)
2. Entity の振舞いの共有
12年8月9日木曜日
![Page 38: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/38.jpg)
Entity の振舞い
• タイムスタンプ管理• 変更のトレース• ソフトデリート可能か• など…
12年8月9日木曜日
![Page 39: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/39.jpg)
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日木曜日
![Page 40: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/40.jpg)
/** * 商品エンティティ */class Product{ use Timestampable, // タイムスタンプを自動で SoftDeletable, // ソフトデリート可能に Activatable; // 活性、非活性を可能に
private $name;
// 本質的なロジック}
12年8月9日木曜日
![Page 41: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/41.jpg)
3
12年8月9日木曜日
![Page 42: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/42.jpg)
時間なかった\(^o^)/
12年8月9日木曜日
![Page 43: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/43.jpg)
いいアイディアあったら教えてください
12年8月9日木曜日
![Page 44: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/44.jpg)
まとめ
12年8月9日木曜日
![Page 45: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/45.jpg)
Trait を知り、PHP 5.4 時代の新たな設計を探ろう
12年8月9日木曜日
![Page 46: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/46.jpg)
それか
12年8月9日木曜日
![Page 47: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/47.jpg)
この会社から PHP を撲滅しよう
12年8月9日木曜日
![Page 48: Trait とは? その使い道を考えてみる](https://reader034.vdocuments.pub/reader034/viewer/2022052207/5550d66fb4c90599308b51cd/html5/thumbnails/48.jpg)
おわり
12年8月9日木曜日