fork download
  1. <?php
  2.  
  3. // your code goes here
  4.  
  5. trait BoxedValue {
  6. private $boxed_value;
  7. public function __construct($value) { $this->boxed_value = $value; }
  8. public function value() { return $this->boxed_value; }
  9. }
  10.  
  11. trait MyBool {
  12. abstract public function isTrue() : bool;
  13. public function isFalse() : bool { return !$this->isTrue(); }
  14. }
  15.  
  16. trait Num {
  17. abstract public function plus($num);
  18. abstract public function negate($num);
  19. public function minus($num) { return $this->plus($num->negate()); }
  20. }
  21.  
  22. class BoolNum {
  23. use BoxedValue, MyBool, Num;
  24.  
  25. public function isTrue() : bool {
  26. return $this->value() != 0;
  27. }
  28.  
  29. public function plus($num) {
  30. return new BoolNum($this->value() + $num->value());
  31. }
  32.  
  33. public function negate() {
  34. return new BoolNum(- $this->value());
  35. }
  36.  
  37. }
  38.  
  39. $a = new BoolNum(-5);
  40. $b = new BoolNum(10);
  41. $c = new BoolNum(0);
  42.  
  43.  
  44. var_dump($a->isTrue());
  45. var_dump($b->isFalse());
  46. var_dump($c->isTrue());
  47. var_dump($c->isFalse());
  48.  
  49.  
  50. var_dump($a->plus($b));
  51. var_dump($a->minus($b));
  52.  
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
bool(true)
bool(false)
bool(false)
bool(true)
object(BoolNum)#4 (1) {
  ["boxed_value":"BoolNum":private]=>
  int(5)
}
object(BoolNum)#5 (1) {
  ["boxed_value":"BoolNum":private]=>
  int(-15)
}