Skip to content

Commit c0857e0

Browse files
DanielEScherzernielsdos
authored andcommitted
Fix GH-17916: Final abstract properties should error
Closes GH-17917.
1 parent 5ede541 commit c0857e0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ PHP NEWS
1717
`__callStatic` is allowed). (timwolla)
1818
. Fixed bug GH-17713 (ReflectionProperty::getRawValue() and related methods
1919
may call hooks of overridden properties). (Arnaud)
20+
. Fixed bug GH-17916 (Final abstract properties should error).
21+
(DanielEScherzer)
2022

2123
- DOM:
2224
. Fixed bug GH-17609 (Typo in error message: Dom\NO_DEFAULT_NS instead of
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-17916: Abstract property cannot be marked as final
3+
--FILE--
4+
<?php
5+
6+
abstract class Foo {
7+
final abstract public string $bar {
8+
get;
9+
}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Cannot use the final modifier on an abstract property in %s on line %d

Zend/zend_compile.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,17 @@ uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifi
10361036
"Multiple readonly modifiers are not allowed", 0);
10371037
return 0;
10381038
}
1039-
if (target == ZEND_MODIFIER_TARGET_METHOD && (new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1040-
zend_throw_exception(zend_ce_compile_error,
1041-
"Cannot use the final modifier on an abstract method", 0);
1042-
return 0;
1039+
if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1040+
if (target == ZEND_MODIFIER_TARGET_METHOD) {
1041+
zend_throw_exception(zend_ce_compile_error,
1042+
"Cannot use the final modifier on an abstract method", 0);
1043+
return 0;
1044+
}
1045+
if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1046+
zend_throw_exception(zend_ce_compile_error,
1047+
"Cannot use the final modifier on an abstract property", 0);
1048+
return 0;
1049+
}
10431050
}
10441051
if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
10451052
if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {

0 commit comments

Comments
 (0)