我正在学习 Marcus Boerger 的标准 PHP 库 (SPL)。
我已经实现了我自己的 RecursiveIterator,它通过继承实现了Iterator
接口。它还实现Countable
.
我对current()、getChildren()和hasChildren方法感到困惑。记录在:http ://www.php.net/~helly/php/ext/spl/interfaceRecursiveIterator.html
如果
current()
引用:'返回当前元素',和getChildren()
返回,我引用,'当前元素的子迭代器'
如果像 的情况一样current()
,当前元素被认为是当前对象的子元素。
然后,文档肯定会指定 getChildren() 实际上返回相关节点的孙子节点。
因此迷茫。
<?php
/**
*@desc Represents a node within a hierarchy
*/
class RecursableCountableIterableNode implements RecursiveIterator, Countable
{
public $title;
private $_componentsArray;
private $_iteratorPosition;
/**
*@desc adds component
*/
public function addComponent(
RecursableCountableIterableNode $incomingNodeObj
)
{
foreach ( $this->_componentsArray as $componentNodeObj )
{
if ( $incomingNodeObj === $componentNodeObj )
{
//its is already in there
return;
}
}
//add to the next element of the numerically indexed array
$this->_componentsArray[] = $incomingNodeObj;
}
/**
* @desc RecursiveIterator Interface
*/
/**
* @desc Implements the RecursiveIterator Interface
* @return boolean - Whether or not the node at the current element
* has children.
*
* Note: This method does NOT count the children of this node,
* it counts the components of the node at the *current* element.
* There is a subtle but important difference.
* It could have been better to name
* the interface method 'hasGrandChildren()'.
*/
public function hasChildren()
{
return ( boolean ) count( $this->current() );
}
/**
* @desc Gets the node of the current element which in effect is a container
* for childnodes.
*
* Note: According to the SPL, it does NOT get 'the child elements of
* the current node' ($this->_componentsArray) which was a surprise to me.
*
* @return RecursableCountableIterableNode - the
* sub iterator for the current element
*
*/
public function getChildren()
{
return $this->current();
}
/**
* @desc To adhere to countable interface.
* @returns integer - The number of elements in the compondents array.
*/
public function count()
{
return count( $this->_componentsArray );
}
/**
* Iterator methods
*/
/**
* @desc Rewind the iterator to the first element.
* @return void
*/
public function rewind()
{
$this->_iteratorPosition = 0;
}
/**
* @desc Return the current element.
* @return RecursableCountableIterableNode
*/
public function current()
{
return $this->_componentsArray[ $this->_iteratorPosition ];
}
/**
* @desc Return the key of the current element.
* @return integer
*/
public function key()
{
return $this->_iteratorPosition;
}
/**
* @desc Move forward to the next element.
* @return void
*/
public function next()
{
++$this->_iteratorPosition;
}
/**
* @desc Checks if current position has an element
* @return boolean
*/
public function valid()
{
return isset( $this->_componentsArray[ $this->_iteratorPosition ] );
}
}
在上面的类中,getChildren()
返回一个实现 RecursiveIterator 和 Countable 的对象。因为每个RecursableCountableIterableNode
对象都包含其他RecursableCountableIterableNode
对象的实例。我认为它是一种复合模式。
通过实验,我设法通过使用count()
(作为退出递归过程的终止条件)并foreach
遍历每个节点的子节点来对树执行递归操作。
有趣的是,实际上,count
特征隐式执行hasChildren
操作,而foreach
构造隐式执行getChildren
操作以执行递归遍历。
class NodeTreeProcessor
{
protected $output = '';
public function doProcessingWithNode(
RecursableCountableIterableNode $treeNodeObj
)
{
$this->output .= $treeNodeObj->title;
//Base case that stops the recursion.
if (!( count( $treeNodeObj ) > 0 ))
{
//it has no children
return;
}
//Recursive case.
foreach( $treeNodeObj as $childNode )
{
$this->doProcessingWithNode( $childNode );
}
}
}
鉴于此,我认为为了成为一个实用的 RecursiveIterator,
getChildren
真的应该返回$this
而不是节点 atcurrent()
,并且hasChildren
真的应该返回布尔转换结果count($this)
这是正确的吗?
规格说明了一件事——我从字面上理解。但我的实践经验说明了另一个问题。