php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37829 set_error_handler used on a $this object causes unexpected result
Submitted: 2006-06-16 21:00 UTC Modified: 2006-06-17 10:38 UTC
From: e at osterman dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.4 OS: Debian
Private report: No CVE-ID: None
 [2006-06-16 21:00 UTC] e at osterman dot com
Description:
------------
This is either a bug in PHP, or an undocumented consequence of the language's design -- either of which should be addressed.

If you want to have an object set an error handler to a method in itself, such that when the object passes out of scope or gets destroyed, the __destruct method is called (and previous error handler restored), you cannot do it. The __destruct method is not called until program termination.

It appears that by setting the error handler to $this->method, causes $this to become copied rather than referenced. Using Array( &$this, 'method' ) versus Array( $this, 'method' ) has no effect on the outcome -- the problem is "problem" is the same.

It's kind'a like creating a scope wormhole, I know, but the documentation doesn't say it's not allowed! :)

Reproduce code:
---------------
class TestErrorHandler
{
    public function __construct()
    {
        print "construct\n";
        set_error_handler( Array( $this, 'handler') );
    }
    public function __destruct()
    {
        print "destruct\n";
        restore_error_handler();
    }
    public function handler()
    {
        print "handled\n";
    }

}

$foo = new TestErrorHandler();
$foo = new TestErrorHandler();

print "done.\n";



Expected result:
----------------
construct
destruct
construct
done.
destruct


Actual result:
--------------
construct
construct
done.
destruct
destruct


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-16 22:38 UTC] e at osterman dot com
Please ignore this bug report. My code example is not correct as re-assigning a variable does not in any way change the scope. I'll resubmit a better example.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 18:01:31 2024 UTC