Posts Tagged ‘nesting-niveau te diep’

PHP Fout Nesting Level Too Deep Recursief Afhankelijkheid

12-3, 2010

Ik heb geïnstalleerd PHP 5.2 op een van mijn computers testen vandaag en een paar stukjes code die voorheen goed werkte in versie 5.1.6 wierp fatale fouten in de nieuwe versie. De foutmelding was "nesting-niveau te diep - recursive afhankelijkheid?"En het duurde een beetje tijd

to track down the root of the problem. Here’s what I’d done wrong.

In PHP there are two comparison operators, == and ===. It’s generally known that the first is not strict about type but the second is. Zo, bij voorbeeld

echo ( false == 0 ); // true

echo ( false === 0 ); // false

- 0 is an integer and false is a boolean

My problem arose from using non-strict typing with objects.

$a = new MyObj();
$b = new MyObj();
als( $a == $b )

I hadn’t considered what I was doing with this code. When comparing two objects using the non-strict comparison operator (==) PHP compares all the properties of the objects and if they match the objects are deemed to be equal. If they don’t match they are not equal. In effect, we have a recursive comparison of all the properties of each object, and all their properties, etc.. until we reach basic data types like strings and integers.

If, echter, we use strict comparison (===), PHP will check whether the two objects are exactly the same object, not just objects with the same properties.

class MyObj
{
public $p;
}

$a = new MyObj();
$b = new MyObj();
$c = new MyObj();
$a->p = 1;
$b->p = 1;
$c->p = 2;
echo ( $a == $c ); // false
echo ( $a == $b ); // true
echo ( $a === $b ); // false

The problem arises if you have circular references in your objects properties. Zo, bij voorbeeld

class MyObj
{
public $p;
}
class OtherObj
{
public $q;
}

$a = new MyObj();
$b = new OtherObj();
$a->p = $b;
$b->q = $a; // the circular reference: $a->p->q === $a

$c = new MyObj();
$d = new OtherObj();
$c->p = $d;
$d->q = $c;// another circular reference: $c->p->q === $c

echo ( $a == $c ); // Fatal error:
Nesting level too deep – recursieve afhankelijkheid?

In order to compare $a to $c, PHP must compare their properties. So the logic in PHP goes something like this: $a == $c if $a->p == $c->p if $a->p->q == $c->p->q if $a->p->q->p == $c->p->q->p etc. indefinitely.

PHP 5.1 seemed to smooth over the problem somehow (probably after a certain level of recursion it simply returned false) – and usually it worked out fine. PHP 5.2 correctly produces the fatal error above.

Once you know the problem, the solution is easyuse strict comparison.

echo ( $a === $c ); // false (and no error)

The strict comparison will simply check whether the two objects are at the same location in memory and so doesn’t even look at the values of the properties.

N.B. The same problem can arise when using the negated comparison operators (use !== instead of !=) and when using in_array (use in_array’s third parameter to indicate strict comparison).

Share and Enjoy

  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency
  • wp socializer sprite mask 16px PHP Error Nesting Level Too Deep Recursive Dependency