fork(3) download
  1. <?php
  2.  
  3. class Node {
  4.  
  5. protected $root = NULL;
  6. protected $seed = array();
  7. protected $name;
  8.  
  9.  
  10. public function __construct($name) {
  11. $this->name = $name;
  12. }
  13. public function getName() {
  14. return $this->name;
  15. }
  16.  
  17. public function getParentNode(){
  18. if (isset($this->root)) {
  19. return $this->root;
  20. }
  21. return NULL;
  22. }
  23. public function getChildrenNode() {
  24. return $this->seed;
  25. }
  26.  
  27. public function getAllParents(){
  28. if (NULL == $this->getParentNode()) {
  29. return array();
  30. } else {
  31. $parent = $this->getParentNode();
  32. $parents = $parent->getAllParents();
  33. $parents[] = $parent;
  34. return $parents;
  35. }
  36. }
  37. public function getAllChildren(){
  38. $childrenNode = $this->getChildrenNode();
  39. $children = $childrenNode;
  40. foreach ($childrenNode as $child) {
  41. $children = array_merge($children, $child->getAllChildren());
  42. }
  43.  
  44. return $children;
  45. }
  46.  
  47. public function getDepth() {
  48. $seeds = $this->seed;
  49. $depth = -1;
  50. //echo $this->name() . '->';
  51. foreach ($seeds as $seed) {
  52. $seedDepth = $seed->getDepth();
  53. if ($depth < $seedDepth) {
  54. $depth = $seedDepth;
  55. }
  56. }
  57. $depth++;
  58. return $depth;
  59. }
  60.  
  61. public function getDescendantsCount(){
  62. $DescendantsCount = 0;
  63. $seeds = $this->seed;
  64. echo $this->name() . '->';
  65. foreach ($seeds as $seed) {
  66. $DescendantsCount++;
  67. $DescendantsCount += $seed->getDescendantsCount();
  68. }
  69. return $DescendantsCount;
  70. }
  71.  
  72. public function appendChild($child) {
  73. //даём ссылку на потомка
  74. $this->seed[] = $child;
  75. $child->appendParent($this);
  76. }
  77. //функция appendParent($child) является продолжение функции appendChild()
  78. //и не имеет смысла её использование вне функции appendChild()
  79. //Не забыть спросить о том, как запретить её вызов другими функциями
  80. //или пользователем
  81. //подобно тому как лычка protected запрещает обращатся к переменной
  82. //из другого обьекта
  83.  
  84. public function appendParent($parent) {
  85. //даём ссылку на корень
  86. $this->root = $parent;
  87. }
  88.  
  89. public function remove(){
  90. if (isset($this->root)) {
  91. $root = $this->root;
  92. $root->removeChild($this);
  93. unset($this->root);
  94. }
  95. }
  96. //функция removeChild($child) является продолжение функции remove()
  97. //и не имеет смысла её использование вне функции remove()
  98. //Не забыть спросить о том, как запретить её вызов другими функциями
  99. //или пользователем.
  100. //подобно тому как лычка protected запрещает обращатся к переменной
  101. //из другого обьекта
  102. public function removeChild($child) {
  103. $seeds = $this->seed;
  104. foreach ($seeds as $seed) {
  105. if ($seed == $child) {
  106. unset($child);
  107. break;
  108. }
  109. }
  110. }
  111.  
  112. public function getNextSibling(){
  113. $root = $this->root;
  114. if (NULL ==$root) {
  115. return NULL;
  116. }
  117. $siblings = $root->getSeed();
  118. foreach ($siblings as $key => $sibling) {
  119. if ($sibling == $this) {
  120. if (isset($siblings[$key+1])) {
  121. return $siblings[$key+1];
  122. } else {
  123. return NULL;
  124. }
  125. }
  126. }
  127. }
  128. public function getPreviousSibling(){
  129. $root = $this->root;
  130. if (NULL ==$root) {
  131. return NULL;
  132. }
  133. $siblings = $root->getSeed();
  134. foreach ($siblings as $key => $sibling) {
  135. if ($sibling == $this) {
  136. if (isset($siblings[$key-1])) {
  137. return $siblings[$key-1];
  138. } else {
  139. return NULL;
  140. }
  141. }
  142. }
  143. }
  144. public function getSeed () {
  145. return $this->seed;
  146. }
  147.  
  148. /*
  149.  
  150.   public function walk(function ($child) {
  151.   ...
  152.   });
  153.   // обходит всех потомков $node, для каждого вызывая функцию
  154.  
  155.   WAT ???
  156.   */
  157. public function walk($function) {
  158. $children = $this->getAllChildren();
  159. foreach ($children as $child) {
  160. echo "\nОперацию над обьектом " . $child->getName()
  161. . " совершает функция {$function}\n";
  162. echo $child->$function();
  163. }
  164. }
  165.  
  166. }
  167.  
  168.  
  169. //создаём узлы 1 -> 2
  170. // -> 3 -> 4
  171. // -> 5
  172. $node1 = new Node('node1');
  173.  
  174. $node2 = new Node('node2');
  175. $node1->appendChild($node2);
  176.  
  177. $node3 = new Node('node3');
  178. $node1->appendChild($node3);
  179.  
  180. $node4 = new Node('node4');
  181. $node3->appendChild($node4);
  182.  
  183. $node5 = new Node('node5');
  184. $node1->appendChild($node5);
  185.  
  186. /*$parents = $node4->getAllParents();
  187.   foreach ($parents as $node) {
  188.   echo $node->getName() . '->';
  189.   }*/
  190.  
  191. /*$names = array();
  192. $children = $node1->getChildrenNode();
  193. foreach ($children as $child) {
  194.   $names[] = $child->getName();
  195. }
  196. print_r ($names);*/
  197.  
  198. /*
  199. $children = $node3->getAllChildren();
  200.   foreach ($children as $node) {
  201.   echo $node->getName() . '->';
  202.   }
  203.   */
  204.  
  205. //echo $node1->getDepth();
  206.  
  207. $function = "getDepth";
  208. $node1->walk($function);
  209.  
  210. //echo "\n" . $node3->getDescendantsCount();
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Операцию над обьектом node2 совершает функция getDepth
0
Операцию над обьектом node3 совершает функция getDepth
1
Операцию над обьектом node5 совершает функция getDepth
0
Операцию над обьектом node4 совершает функция getDepth
0