fork(1) download
  1. <?php
  2.  
  3. class Organisation {
  4.  
  5. private $name;
  6. private $departments = [];
  7.  
  8. public function __construct($name) {
  9. $this->name = $name;
  10. }
  11.  
  12. public function addDepartment(Department $dep) {
  13.  
  14. $exist = false;
  15.  
  16. foreach ($this->$departments as $department) {
  17. if ($department === $dep) {
  18. $exist = true;
  19. }
  20. }
  21.  
  22. if ($exist == false) {
  23. $this->departments[] = $dep;
  24. }
  25.  
  26. }
  27.  
  28.  
  29. }
  30.  
  31. class Department {
  32. public function __construct($name) {
  33. $this->name = $name;
  34. }
  35. }
  36.  
  37. $vector = new Organisation('Вектор');
  38.  
  39. $dep1 = new Department('dep1');
  40. $vector->addDepartment($dep1);
  41. $vector->addDepartment($dep1);
  42.  
  43. var_dump($vector);
  44.  
Runtime error #stdin #stdout #stderr 0s 52488KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Notice:  Undefined variable: departments in /home/VHTPaI/prog.php on line 16
PHP Fatal error:  Cannot access empty property in /home/VHTPaI/prog.php on line 16