fork(1) download
  1. <?php
  2. abstract class A
  3. {
  4. public function __construct() {}
  5.  
  6. abstract protected function listOfValues();
  7.  
  8. public function sum() {
  9. return array_sum($this->listOfValues());
  10. }
  11. }
  12.  
  13. class new_B extends A {
  14. public $one = 1;
  15. public $two = 2;
  16. public $three = 3;
  17. public $four = 4;
  18.  
  19. public function __construct() {
  20. parent::__construct();
  21. }
  22.  
  23. protected function listOfValues() {
  24. return array(
  25. $this->one,
  26. $this->two,
  27. $this->three,
  28. $this->four
  29. );
  30. }
  31. }
  32. class new_C extends A {
  33. public $one = 1;
  34. public $two = 2;
  35.  
  36. public function __construct() {
  37. parent::__construct();
  38. }
  39. protected function listOfValues() {
  40. return array(
  41. $this->one,
  42. $this->two
  43. );
  44. }
  45. }
  46.  
  47. $b = new new_B();
  48. echo $b->sum();
  49. echo "\n";
  50. $c = new new_C();
  51. echo $c->sum();
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
10
3