Skip to content

Commit 663536d

Browse files
authored
Improve class inheritance error messages (php#7307)
1 parent a374230 commit 663536d

File tree

12 files changed

+20
-20
lines changed

12 files changed

+20
-20
lines changed

Zend/tests/enum/final.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class Bar extends Foo {}
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Class Bar may not inherit from final class (Foo) in %s on line %d
12+
Fatal error: Class Bar cannot extend final class Foo in %s on line %d

Zend/tests/generators/errors/generator_extend_error.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class ExtendedGenerator extends Generator { }
77

88
?>
99
--EXPECTF--
10-
Fatal error: Class ExtendedGenerator may not inherit from final class (Generator) in %s on line %d
10+
Fatal error: Class ExtendedGenerator cannot extend final class Generator in %s on line %d

Zend/tests/traits/error_001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ class A extends foo {
2525

2626
?>
2727
--EXPECTF--
28-
Fatal error: Class A cannot extend from trait foo in %s on line %d
28+
Fatal error: Class A cannot extend trait foo in %s on line %d

Zend/tests/traits/error_009.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class foo extends abc { }
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Class foo cannot extend from trait abc in %s on line %d
12+
Fatal error: Class foo cannot extend trait abc in %s on line %d

Zend/tests/weakrefs/weakrefs_004.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ WeakReference no inheritance
55
class Test extends WeakReference {}
66
?>
77
--EXPECTF--
8-
Fatal error: Class Test may not inherit from final class (WeakReference) in %s on line %d
8+
Fatal error: Class Test cannot extend final class WeakReference in %s on line %d

Zend/zend_inheritance.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -1424,19 +1424,19 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
14241424
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_INTERFACE)) {
14251425
/* Interface can only inherit other interfaces */
14261426
if (UNEXPECTED(!(parent_ce->ce_flags & ZEND_ACC_INTERFACE))) {
1427-
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s may not inherit from class (%s)", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1427+
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s cannot extend class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
14281428
}
14291429
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL))) {
1430-
/* Class declaration must not extend traits or interfaces */
1431-
if (parent_ce->ce_flags & ZEND_ACC_INTERFACE) {
1432-
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend from interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1433-
} else if (parent_ce->ce_flags & ZEND_ACC_TRAIT) {
1434-
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend from trait %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1435-
}
1436-
14371430
/* Class must not extend a final class */
14381431
if (parent_ce->ce_flags & ZEND_ACC_FINAL) {
1439-
zend_error_noreturn(E_COMPILE_ERROR, "Class %s may not inherit from final class (%s)", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1432+
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend final class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
1433+
}
1434+
1435+
/* Class declaration must not extend traits or interfaces */
1436+
if ((parent_ce->ce_flags & ZEND_ACC_INTERFACE) || (parent_ce->ce_flags & ZEND_ACC_TRAIT)) {
1437+
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend %s %s",
1438+
ZSTR_VAL(ce->name), parent_ce->ce_flags & ZEND_ACC_INTERFACE ? "interface" : "trait", ZSTR_VAL(parent_ce->name)
1439+
);
14401440
}
14411441
}
14421442

ext/imap/tests/imap_final.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ imap
77

88
class T extends IMAP\Connection {}
99
--EXPECTF--
10-
Fatal error: Class T may not inherit from final class (IMAP\Connection) in %s on line %d
10+
Fatal error: Class T cannot extend final class IMAP\Connection in %s on line %d

ext/reflection/tests/ReflectionAttribute_final.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class T extends ReflectionAttribute {}
77

88
?>
99
--EXPECTF--
10-
Fatal error: Class T may not inherit from final class (ReflectionAttribute) in %s on line %d
10+
Fatal error: Class T cannot extend final class ReflectionAttribute in %s on line %d

ext/standard/tests/class_object/bug78638.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ $c = new class('bar') extends __PHP_Incomplete_Class {
66
};
77
?>
88
--EXPECTF--
9-
Fatal error: Class __PHP_Incomplete_Class@anonymous may not inherit from final class (__PHP_Incomplete_Class) in %s on line %d
9+
Fatal error: Class __PHP_Incomplete_Class@anonymous cannot extend final class __PHP_Incomplete_Class in %s on line %d

ext/xml/tests/bug78563_final.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ class Dummy extends Xmlparser {
1212
?>
1313
===DONE===
1414
--EXPECTF--
15-
Fatal error: Class Dummy may not inherit from final class (XMLParser) in %s on line %d
15+
Fatal error: Class Dummy cannot extend final class XMLParser in %s on line %d

tests/classes/class_final.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ class derived extends base {
1717
echo "Done\n"; // shouldn't be displayed
1818
?>
1919
--EXPECTF--
20-
Fatal error: Class derived may not inherit from final class (base) in %s on line %d
20+
Fatal error: Class derived cannot extend final class base in %s on line %d

tests/classes/interface_and_extends.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ $o->show();
2121
?>
2222
===DONE===
2323
--EXPECTF--
24-
Fatal error: Class Tester cannot extend from interface Test in %sinterface_and_extends.php on line %d
24+
Fatal error: Class Tester cannot extend interface Test in %sinterface_and_extends.php on line %d

0 commit comments

Comments
 (0)