Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Immutable
- {
- private $a;
- private $b;
- private $c;
- public function __construct($a, $b, $c) {
- $this->a = $a;
- $this->b = $b;
- $this->c = $c;
- }
- //get methods
- //...
- public function __toString() { return "{$this->a} : {$this->b} : {$this->c}"; }
- public function prototype(): ImmutablePrototype {
- return new ImmutablePrototype($this->a, $this->b, $this->c);
- }
- }
- class ImmutablePrototype
- {
- private $a;
- private $b;
- private $c;
- //some data structure would be more common
- public function __construct($a, $b, $c) {
- $this->a = $a;
- $this->b = $b;
- $this->c = $c;
- }
- //set methods
- public function a(string $value): Immutable { $this->a = $a; return $this; }
- public function b(string $value): Immutable { $this->b = $b; return $this; }
- public function c(string $value): Immutable { $this->c = $c; return $this; }
- //validation might happen here if it's costly
- //late failure makes debugging harder, but you don't often pass builder around
- public function build(): Immutable {
- return new Immutable($this->a, $this->b, $this->c);
- }
- }
Add Comment
Please, Sign In to add comment