Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php declare(strict_types=1);
  2.  
  3. // /app/family/Father.php
  4. namespace app\family;
  5.  
  6. class Father
  7. {
  8.     protected $name;
  9.     protected function with(string $name, $value): Father
  10.     {
  11.         $new = clone $this;
  12.         $new->{$name} = $value;
  13.         return $new;
  14.     }
  15.     public function withName(string $name): Father { return $this->with('name', $name); }
  16.     public function __toString(): string { return "{$this->name}\n"; }
  17. }
  18.  
  19. // /app/family/Daughter.php
  20. namespace app\family;
  21.  
  22. class Daughter extends Father
  23. {
  24.     protected $hair;
  25.     protected $name;
  26.     public function __toString(): string { return "{$this->name} has {$this->hair} hair.\n"; }
  27.     public function withHair(string $hair): Daughter { return $this->with('hair', $hair); }
  28. }
  29.  
  30. // Some usage=
  31. use app\family\Daughter;
  32. $d = new Daughter;
  33. $d1 = $d->withHair('golden')->withName('Sofia');
  34. $d2 = $d->withName('Mari')->withHair('dark');
  35. echo $d1 . $d2 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement