Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types=1);
- // /app/family/Father.php
- namespace app\family;
- class Father
- {
- protected $name;
- protected function with(string $name, $value): Father
- {
- $new = clone $this;
- $new->{$name} = $value;
- return $new;
- }
- public function withName(string $name): Father { return $this->with('name', $name); }
- public function __toString(): string { return "{$this->name}\n"; }
- }
- // /app/family/Daughter.php
- namespace app\family;
- class Daughter extends Father
- {
- protected $hair;
- protected $name;
- public function __toString(): string { return "{$this->name} has {$this->hair} hair.\n"; }
- public function withHair(string $hair): Daughter { return $this->with('hair', $hair); }
- }
- // Some usage=
- use app\family\Daughter;
- $d = new Daughter;
- $d1 = $d->withHair('golden')->withName('Sofia');
- $d2 = $d->withName('Mari')->withHair('dark');
- echo $d1 . $d2 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement