in_array() related problem
| From: | tamcy | Date: | Fri, 03 Nov 2006 16:56:27 +0000 |
| Subject: | in_array() related problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-244159@lists.php.net to get a copy of this message | ||
Hello all,
I'm new to this list. To not flooding the bug tracking system I hope
to clarify some of my understanding here.
I am referring to the (now bogus) bug report
http://bugs.php.net/bug.php?id=39356&edit=2. This happens after my
upgrade to PHP 5.2, where the code shown produces a "Fatal error:
Nesting level too deep - recursive dependency?". Same testing code
reproduced below:
----------------------------
<?php
class A
{
public $b;
}
class B
{
public $a;
}
$a = new A;
$b = new B;
$b->a = $a;
$a->b = $b;
$test = array($a, $b);
var_dump(in_array($a, $test));
----------------------------
I think this is not rare for a child item to have knowledge about its
parent, forming a cross-reference.
This code runs with no problem in PHP5.1.6, but not in 5.2. Ilia
kindly points out that "In php 5 objects are passed by reference, so
your code does in
fact create a circular dependency.". I know the passed by reference
rule. What I'm now puzzled is, why this should lead to an error.
To my knowledge, despite the type-casting issue and actual algorithm,
in_array() should actually do nothing more than:
function mimic_in_array($search, $list)
{
foreach ($list as $item)
if ($search == $item)
return true;return false; } Which means: 1. in_array() isn't multi-dimensional. 2. in_array() doesn't care about the properties of any object. That is, I don't expect in_array() to nest through all available inner arrays for a match, not to mention those are object properties, not arrays. So here is the question: Why should in_array() throws such a "Fatal error: Nesting level too deep" error? Why should it care? Is there any behaviour I don't know? Thanks all in advance. Tamcy