Skip to content

Commit d88b212

Browse files
committed
Improve "already declared" error message
If this error is missing because the rtd_key was renamed to lcname, fetch the class based on lcname and use the class type and cased name from there.
1 parent fbe287a commit d88b212

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Cannot declare class, because the name is already in use
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
class A {}
8+
}
9+
test();
10+
test();
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Cannot declare class A, because the name is already in use in %s on line %d

Zend/zend_compile.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,12 +1067,9 @@ ZEND_API int do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
10671067
zv = zend_hash_find_ex(EG(class_table), Z_STR_P(rtd_key), 1);
10681068

10691069
if (UNEXPECTED(!zv)) {
1070-
/* If we're in compile time, in practice, it's quite possible
1071-
* that we'll never reach this class declaration at runtime,
1072-
* so we shut up about it. This allows the if (!defined('FOO')) { return; }
1073-
* approach to work.
1074-
*/
1075-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s, because the name is already in use", Z_STRVAL_P(lcname));
1070+
ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1071+
ZEND_ASSERT(ce && "Class with lcname should be registered");
1072+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
10761073
return FAILURE;
10771074
}
10781075

0 commit comments

Comments
 (0)