php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35634 Erroneous "Class declarations may not be nested" error raised.
Submitted: 2005-12-11 19:15 UTC Modified: 2007-01-11 16:48 UTC
Votes:16
Avg. Score:4.5 ± 0.7
Reproduced:13 of 14 (92.9%)
Same Version:6 (46.2%)
Same OS:9 (69.2%)
From: robert at interjinn dot com Assigned: dmitry
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.1 OS: Linux
Private report: No CVE-ID:
 [2005-12-11 19:15 UTC] robert at interjinn dot com
Description:
------------
PHP bails out with class declaration nesting error when an error handler dynamically loads an error handling class during a class related E_STRICT warning.

Reproduce code:
---------------
test.php
<?php

set_error_handler( 'errorHandler' );

function errorHandler
( $errorNumber, $errorMessage, $fileName, $lineNumber )
{
    require_once( 'errorClass.php' );
    $error = new ErrorClass();

    $error->handleException(
        $errorNumber, $errorMessage, $fileName, $lineNumber );
}


require_once( 'testClass.php' );

$test = new TestClass();
?>

-------------------------------------------------

testClass.php
<?php

class TestClass
{
    function __construct()
    {
    }

    function TestClass()
    {
        $this->__construct();
    }
}

?>

-------------------------------------------------

errorClass.php
<?php

class ErrorClass
{
    function handleException
    ( $errorNumber, $errorMessage, $fileName, $lineNumber )
    {
        echo "Error: [$errorNumber] $errorMessage ($fileName:$lineNumber)\n";
    }
}

?>



Expected result:
----------------
I expect to properly be able to handle the following E_STRICT in my custom error class:

<br />
<b>Strict Standards</b>:  Redefining already defined constructor for class TestClass in <b>/home/suds/testClass.php</b> on line <b>9</b><br />



Actual result:
--------------
Fatal error:  Class declarations may not be nested in /home/suds/errorClass.php on line 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-16 20:22 UTC] robert at interjinn dot com
I think this report should be given further consideration. I've read the docs (not that I hadn't before), and I will assume you think this is bogus because of the following int he PHP docs:

    The following error types cannot be handled with
    a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR,
    E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and
    most of E_STRICT raised in the file where
    set_error_handler() is called.

However, the E_STRICT raised int he error handler script should not be raised in the first place since it is PHP erroneous context that believes the class declaration is being nested, when in fact the class declaration is not being nested. If the include context was properly scoped, then no E_STRICT would be raised and I wouldn't be having a problem.

If this is not why this bug was marked bogus, please shed some light for me, since the bogus comment was pretty uninformative (yes I know you're busy and don't have time for 50 million bogus bugs but there's only so much RTFM I can do and hope I can come across exactly what you think makes this a bogus bug).

Cheers,
Rob.
 [2006-08-08 16:25 UTC] rabbitt at gmail dot com
if you think about it, it actually makes sense (and it doesn't at the same time). When the exception is triggered, it's done from within TestClass (during the declaration of the class (zend_do_begin_function_declaration())), so the scope is still focused there. So when errorHandler() is called due to the E_STRICT, it is including (and thus, declaring) the 'errorClass' inside of the errorHandler() function *inside* of TestClass.

Personally, I think this is a bug and, that the active_class_entry should be saved and cleared prior to calling the errorhandler and then restored afterwards. That would enable the error handler to work with errors that are emitted within the context of a class that's in the process of being declared (ie: scope is inbetween zend_do_begin_class_declaration() and zend_do_end_class_declaration()).

I'll attach a quick patch that does just this momentarily.
 [2006-08-14 17:27 UTC] tony2001@php.net
http://www.xaraya.com/~rabbitt/zend.c.patch
Dmitry, could you plz check out this patch?
 [2006-11-08 01:59 UTC] a dot laquerre at videotron dot ca
I have upgraded to from version 5.1 to 5.2 on Windows with Apache and I have an that error "Class declarations may not be nested".

I had that bug in that case:

class A extends C{

 static public function foo{
 
  require_once(B.class);
 }

}
 [2006-11-30 15:22 UTC] rabbitt at gmail dot com
Dmitry - just curious if you have had a chance to look at this patch? I had discussed it with Sara G. originally, so if you need background I'm sure you could speak with her (don't know for sure, haven't spoken to her about it since), or contact me directly.

cheers!,

--
Carl
 [2007-01-11 16:48 UTC] dmitry@php.net
The patch is applied into CVS HEAD and PHP_5_2.
 [2009-11-17 11:18 UTC] svn@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&revision=290862
Log: - Fixed bug #50145 (crash while running bug35634.phpt)
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 07:02:14 2014 UTC