Created
June 21, 2010 15:48
-
-
Save jakubkulhan/447047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jak nejlépe na tree walker v PHP? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* + kratší | |
* + není potřeba duplikovat kód v případě, že se více uzlů zpracovává | |
* stejně -- propadávání | |
* + funguje jak v PHP 5.3, tak 5.2 | |
* | |
* - horší čitelnost | |
* - nedeklarativní | |
* - obtížné udržování stavu | |
*/ | |
function my_tree_walker($tree) | |
{ | |
switch ($tree[0]) { | |
case 'A': | |
list($_, $subnodes) = $tree; | |
$ret = ''; | |
foreach ($subnodes as $subnode) { | |
$ret .= my_tree_walker($subnode); | |
} | |
return $ret; | |
break; | |
case 'B': /*...*/ break; | |
case 'C': /*...*/ break; | |
case 'D': /*...*/ break; | |
case 'E': /*...*/ break; | |
case 'F': /*...*/ break; | |
case 'G': /*...*/ break; | |
case 'H': /*...*/ break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* + lépší čitelnost | |
* + deklarativnější | |
* + snadné udržování stavu | |
* | |
* - delší | |
* - je nutné duplikovat kód, v případě, že se více uzlů zpracovává stejně | |
* (připadně jednou implementovat a volat z ostatních tuto metodu -- nepěkné) | |
* - `new static` až od PHP 5.3 | |
*/ | |
abstract class TreeWalker | |
{ | |
protected function __construct(){} | |
final protected function walk($node) | |
{ | |
$nodetype = array_shift($node); | |
return call_user_func_array(array($this, 'walk' . $nodetype), $node); | |
} | |
public static function w($tree) | |
{ | |
$instance = new static; | |
return $instance->walk($tree); | |
} | |
} | |
final class MyTreeWalker extends TreeWalker | |
{ | |
protected function __construct() | |
{ | |
// initialize | |
} | |
private function walkA($subnodes) | |
{ | |
$ret = ''; | |
foreach ($subnodes as $subnode) { | |
$ret .= $this->walk($subnode); | |
} | |
return $ret; | |
} | |
private function walkB($n) { /*...*/ } | |
private function walkC($n) { /*...*/ } | |
private function walkD($subnode) { /*...*/ } | |
private function walkE() { /*...*/ } | |
private function walkF() { /*...*/ } | |
private function walkG() { /*...*/ } | |
private function walkH($n) { /*...*/ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tree = array("A", array( | |
array("B", 1), | |
array("C", 2), | |
array("D", | |
array("E", | |
array("F")), | |
array("G")), | |
array("H", 3))); | |
return $tree; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment