php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38866 Nesting level too deep Error when Object Contains Self Reference
Submitted: 2006-09-18 14:37 UTC Modified: 2006-09-18 16:00 UTC
From: adam at sccode dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5CVS-2006-09-18 (snap) OS: Windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: adam at sccode dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-18 15:11 UTC] tony2001@php.net
With $this->attr = $this you get recursive references, so the error message is expected.
 [2006-09-18 15:21 UTC] adam at sccode dot com
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.
 [2006-09-18 15:35 UTC] tony2001@php.net
After this change I get only bool(false).
What is the problem?
 [2006-09-18 15:56 UTC] adam at sccode dot com
You're right!! I am having a bad day - I was changing the wrong file all along. I'm very sorry to have wasted your time.
 [2006-09-18 16:00 UTC] johannes@php.net
No PHP problem -> bogus
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 21:01:35 2025 UTC