|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-09-18 14:37 UTC] adam at sccode dot com
Description:
------------
Attempting to compare two variables containing a reference to an object instance which in turn contains a property static or otherwise, referring back to itself produces a "Nesting level too deep" fatal error. One example where a self references may be necessary is a singleton: in PHP 5.1.6 the reproduce code works as expected. A feature, or a bug?
Reproduce code:
---------------
$obj = Singleton::instance();
$obj2 = $obj;
var_dump($obj != $obj2);
class Singleton
{
private static $instance;
private function __construct()
{
$this->me = $this;
}
public static function instance()
{
if (! self::$instance) {
self::$instance = new Singleton;
}
return self::$instance;
}
}
Expected result:
----------------
bool(false)
Actual result:
--------------
Fatal error: Nesting level too deep - recursive dependency? in crash-test.php on line 5
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 07:00:01 2025 UTC |
Apologies ... I posted the wrong reproduce code: private function __construct() { $this->me = $this; } Should read: private function __construct() { self::$instance = $this; } In previous versions of PHP this has produced no error. There are sometimes occasions where checking if two varaibles reference the same object instance is necessaary - I don't think a static property should break this.