PHP 5.2 出现的Nesting level too deep - recursive dependency问题
2006-12-21 10:41
323 查看
在PHP 5.1下运行很好的PHP,移植到PHP5.2之后出现 Nesting level too deep - recursive dependency的问题。
对PHP我是不懂了,但让其正常运行是我的责任,于是google。
下面是找到的东西
http://www.phpdeveloper.org/news/6691
So, you've just upgraded to PHP 5.2 and all is going well until you come across a page in your application that gives the message "Nesting level too deep - recursive dependency?". With such a vague error message, you might have trouble locating the source of the problem. Thankfully, someone's already been there and figured out the issue - Richard Lord. I installed PHP 5.2 on one of my testing servers today and a couple of bits of code that previously worked fine in version 5.1.6 threw fatal errors in the new version. The error message was "Nesting level too deep - recursive dependency?" and it took a little time to track down the root of the problem. Here's what I'd done wrong.
Basically, his problem was using the "non-strict" evaluation for checking if two objects were equal to each other (== instead of ===). This compares everything about them, down to the properties - even if they're references to other properties inside of the same class (which is where the problem lies).
So, the fix is simple - === instead of == when comparing those objects. You'll be happier for the change.
http://www.bigroom.co.uk/blog/php-nesting-level-too-deep-recursive-dependency/
I installed PHP 5.2 on one of my testing servers today and a couple of bits of code that previously worked fine in version 5.1.6 threw fatal errors in the new version. The error message was “Nesting level too deep - recursive dependency?” and it took a little time to track down the root of the problem. Here’s what I’d done wrong.
In PHP there are two comparison operators,
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();
if( $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, however, 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. So, for example
class MyObj
{
public $p;
}
class OtherObj
{
public $q;
}
$a = new MyObj();
$b = new OtherObj();
$a->p = $b;
$b->q = $a; // the circular reference:
In order to compare $a to $c, PHP must compare their properties. So the logic in PHP goes something like this:
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 easy - use 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
总之问题是解决了,是因为语言不规范所致。将==转为===, 将!=转为!==,彻底好了。
对PHP我是不懂了,但让其正常运行是我的责任,于是google。
下面是找到的东西
http://www.phpdeveloper.org/news/6691
So, you've just upgraded to PHP 5.2 and all is going well until you come across a page in your application that gives the message "Nesting level too deep - recursive dependency?". With such a vague error message, you might have trouble locating the source of the problem. Thankfully, someone's already been there and figured out the issue - Richard Lord. I installed PHP 5.2 on one of my testing servers today and a couple of bits of code that previously worked fine in version 5.1.6 threw fatal errors in the new version. The error message was "Nesting level too deep - recursive dependency?" and it took a little time to track down the root of the problem. Here's what I'd done wrong.
Basically, his problem was using the "non-strict" evaluation for checking if two objects were equal to each other (== instead of ===). This compares everything about them, down to the properties - even if they're references to other properties inside of the same class (which is where the problem lies).
So, the fix is simple - === instead of == when comparing those objects. You'll be happier for the change.
http://www.bigroom.co.uk/blog/php-nesting-level-too-deep-recursive-dependency/
I installed PHP 5.2 on one of my testing servers today and a couple of bits of code that previously worked fine in version 5.1.6 threw fatal errors in the new version. The error message was “Nesting level too deep - recursive dependency?” and it took a little time 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. So, for example
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();
if( $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, however, 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. So, for example
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->p === $a $c = new MyObj(); $d = new OtherObj(); $c->p = $d; $d->q = $c;// another circular reference: $c->p->p === $c echo ( $a == $c ); // Fatal error: Nesting level too deep - recursive dependency?
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->p == $c->p->p if $a->p->p->p == $c->p->p->petc. 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 easy - use 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).
总之问题是解决了,是因为语言不规范所致。将==转为===, 将!=转为!==,彻底好了。
相关文章推荐
- Nesting level too deep
- PHP Maximum function nesting level of '100' reached 问题
- [置顶] ZendFramework1.0 PDF开发中-Nesting level too deep的解决
- PHP5.2 $arr = [] 初始化数组出现问题
- 关于PHP出现Too many connections 的问题
- PHP中出现“Unable to load dynamic library”问题的解决方法
- php中出现“Call to a member function Execute() on a non-object”的问题
- 解决nginx+php(php-fpm)出现502 bad gateway问题
- bat 执行 php 出现问题 原因php 引用 相对路径
- php读取excel文件的日期时间出现类似41395的问题解决
- php出现Cannot modify header information问题
- IIS7+PHP配置时出现FastCGI进程意外退出问题解决
- PHP将数组转为json出现的中文乱码的问题
- CentOS用yum更新php至php5.2(解决PHP 5.2+ is required问题)
- Ecshop出现问题 includes\lib_main.php on line 1329 includes\lib_base.php on line
- 关于PHP集成环境session_start出现错误的问题
- LNMP之编译安装PHP出现的问题
- centOs6环境nginx+php环境出现“Access denied”问题处理
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
- 也说php从mysql数据库通过服务器端json返回数据出现乱码问题