Skip to content

Commit 2e1a243

Browse files
committed
Fixed bug #35634 (Erroneous "Class declarations may not be nested" error raised). (Carl P. Corliss)
1 parent 870bd0b commit 2e1a243

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ PHP NEWS
3333
not found"). (Ilia)
3434
- Fixed bug #36214 (__get method works properly only when conditional operator
3535
is used). (Dmitry)
36+
- Fixed bug #35634 (Erroneous "Class declarations may not be nested" error
37+
raised). (Carl P. Corliss, Dmitry)
3638
- Fixed bug #35106 (nested foreach fails when array variable has a reference).
3739
(Dmitry)
3840

Zend/tests/bug35634.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Bug #35634 (Erroneous "Class declarations may not be nested" error raised)
3+
--INI--
4+
error_reporting=0
5+
--FILE--
6+
<?php
7+
if (defined("pass3")) {
8+
9+
class ErrorClass {
10+
}
11+
12+
} else if (defined("pass2")) {
13+
14+
class TestClass {
15+
function __construct() {
16+
}
17+
function TestClass() {
18+
$this->__construct();
19+
}
20+
}
21+
22+
} else {
23+
24+
function errorHandler($errorNumber, $errorMessage, $fileName, $lineNumber) {
25+
define("pass3", 1);
26+
include(__FILE__);
27+
die("Error: $errorMessage ($fileName:$lineNumber)\n");
28+
}
29+
30+
set_error_handler('errorHandler');
31+
define("pass2", 1);
32+
include(__FILE__);
33+
}
34+
?>
35+
--EXPECTF--
36+
Error: Redefining already defined constructor for class TestClass (%sbug35634.php:12)

Zend/zend.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ ZEND_API void zend_error(int type, const char *format, ...)
900900
char *error_filename;
901901
uint error_lineno;
902902
zval *orig_user_error_handler;
903+
zend_bool in_compilation;
904+
zend_class_entry *saved_class_entry;
903905
TSRMLS_FETCH();
904906

905907
/* Obtain relevant filename and lineno */
@@ -1007,7 +1009,18 @@ ZEND_API void zend_error(int type, const char *format, ...)
10071009

10081010
orig_user_error_handler = EG(user_error_handler);
10091011
EG(user_error_handler) = NULL;
1010-
1012+
1013+
/* User error handler may include() additinal PHP files.
1014+
* If an error was generated during comilation PHP will compile
1015+
* such scripts recursivly, but some CG() variables may be
1016+
* inconsistent. */
1017+
1018+
in_compilation = zend_is_compiling(TSRMLS_C);
1019+
if (in_compilation) {
1020+
saved_class_entry = CG(active_class_entry);
1021+
CG(active_class_entry) = NULL;
1022+
}
1023+
10111024
if (call_user_function_ex(CG(function_table), NULL, orig_user_error_handler, &retval, 5, params, 1, NULL TSRMLS_CC)==SUCCESS) {
10121025
if (retval) {
10131026
if (Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval) == 0) {
@@ -1020,6 +1033,10 @@ ZEND_API void zend_error(int type, const char *format, ...)
10201033
zend_error_cb(type, error_filename, error_lineno, format, args);
10211034
}
10221035

1036+
if (in_compilation) {
1037+
CG(active_class_entry) = saved_class_entry;
1038+
}
1039+
10231040
if (!EG(user_error_handler)) {
10241041
EG(user_error_handler) = orig_user_error_handler;
10251042
}

0 commit comments

Comments
 (0)