Guest User

Untitled

a guest
Sep 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Immutable
  4. {
  5.     private $a;
  6.     private $b;
  7.     private $c;
  8.  
  9.     public function __construct($a, $b, $c) {
  10.         $this->a = $a;
  11.         $this->b = $b;
  12.         $this->c = $c;
  13.     }
  14.    
  15.     //get methods
  16.     //...
  17.     public function __toString() { return "{$this->a} : {$this->b} : {$this->c}"; }
  18.  
  19.     public function prototype(): ImmutablePrototype {
  20.         return new ImmutablePrototype($this->a, $this->b, $this->c);
  21.     }
  22. }
  23.  
  24. class ImmutablePrototype
  25. {
  26.     private $a;
  27.     private $b;
  28.     private $c;
  29.  
  30.     //some data structure would be more common
  31.     public function __construct($a, $b, $c) {
  32.         $this->a = $a;
  33.         $this->b = $b;
  34.         $this->c = $c;
  35.     }
  36.  
  37.     //set methods
  38.     public function a(string $value): Immutable { $this->a = $a; return $this; }
  39.     public function b(string $value): Immutable { $this->b = $b; return $this; }
  40.     public function c(string $value): Immutable { $this->c = $c; return $this; }
  41.  
  42.     //validation might happen here if it's costly
  43.     //late failure makes debugging harder, but you don't often pass builder around
  44.     public function build(): Immutable {
  45.         return new Immutable($this->a, $this->b, $this->c);
  46.     }
  47. }
Add Comment
Please, Sign In to add comment