1

我正在学习 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而不是节点 at current(),并且
  • hasChildren真的应该返回布尔转换结果count($this)

这是正确的吗?

规格说明了一件事——我从字面上理解。但我的实践经验说明了另一个问题。

4

1 回答 1

1

我认为说“孙子”是不对的。您只是将您的参考点从当前迭代器的一个元素更改为当前迭代,这使得孩子们变成了大孩子。我看不出这样做的充分理由,因为这不是我习惯于使用 spl 迭代器的约定。

我建议您坚持使用您发布的代码,但我认为您可能不知道RecursiveIteratorIterator。RecursiveIteratorIterator 旨在处理调用 hasChildren() 和 getChildren() 的复杂性,并在此过程中维护适当的迭代器堆栈。最后,您会看到似乎是您的层次结构的扁平列表。您的 NodeTreeProcessor 类目前正在做一些这样的事情。然后,您可以在 RecursiveIteratorIterator 上进行 foreach,根据您使用的标志,您将获得广度优先或深度优先迭代。不过,您不必使用RecursiveIteratorIterator。

此外,考虑在调用 getChildren() 时返回一个新的外部迭代器。否则,您将放弃一次使用多个迭代器迭代同一节点的可能性,因为您的迭代器的位置将是共享状态。目前,您正在使用内部迭代器范例,其中数据和迭代状态都存储在同一个对象中。外部迭代器将迭代状态与数据分离,让您在同一条数据上拥有超过 1 个活动迭代器。

于 2013-07-06T15:58:00.870 回答