Re: error message: Nesting level too deep - recursive dependency?
| From: | Richard Lynch | Date: | Sun, 25 Aug 2002 00:57:39 +0000 |
| Subject: | Re: error message: Nesting level too deep - recursive dependency? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114012@lists.php.net to get a copy of this message | ||
>class ListItem {
> var $prev;
> var $next;
> var $data;
>
> function ListITem() {
> $this->prev = NULL;
> $this->next = NULL;
> $this->data = NULL;
> }
>}
Why can't you just use a PHP array?...
>class List2 {
> var $first;
> var $last;
> var $current;
> var $length;
>
> function List2()
> {
> $this->first = NULL;
> $this->last = NULL;
> $this->current = NULL;
> $this->length = 0;
> }
>
> function Add(&$item)
> {
> if (is_null($this->first)) {
> $item->prev = NULL;
> $item->next = NULL;
> $this->first = &$item;
> $this->last = &$item;
> } else {
> $item->prev = &$this->last;
> $item->next = NULL;
> $item->prev->next = &$item;
Not sure this twice-removed reference will work...
Try this:
$temp = &$this->last;
$temp->next = &$item;
> $this->last = &$item;
> }
> $this->length++;
> }
Ye Gods!
You should *definitely* be using an array!
:-)
> function &item($index)
> {
> $i = 0;
>
> if ($index >= 0 && $index <= $this->length) {
If $i == $this->length you should be returning NULL.
Instead, you return $item for both $this->length - 1 and $this->length, I
think...
> $item = $this->first;
> while ($index - $i > 0) {
> $item = $item->next;
> $i++;
> }
> } else $item = NULL;
>
> return $item;
> }
>
> function findItem(&$item)
> {
> for ($i = 0;$i < $this->length;$i++) {
> $current = $this->item($i);
>
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> !! Here is a problem !!
> !! this comparation produced an error message !!
> !! "Nesting level too deep - recursive dependency?" !!
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> if($current == $item) return true;
Well, the odds are really good that at some point you are hitting this
situation:
$current->next == $item
$item->prev == $current
When that happens, and you try to use == on $current and $item, PHP goes
into a tail-spin trying to compare the two objects, since each refers to the
other...
In order to compare the two objects, it has to look inside ->next and
->prev, but they each refer to the other, so it looks inside them again, and
has to look into ->prev and ->next and they refer to each other so it looks
inside them again, and again, and again, and then it blows up eventually.
Give every object a unique ID http://php.net/uniqid or
something, and
compare their IDs.
> }
> return false;
> }
>}
It still boggles my mind that you'd want to even *do* all this when you have
a built-in array type that can do everything you've done about a zillion
times faster...
--
Like Music? http://l-i-e.com/artists.htm