Skip to content

Commit 79d024a

Browse files
authored
Allow final modifier when using a method from a trait (#11394)
Fixes GH-11388. Following https://wiki.php.net/rfc/horizontalreuse which introduced traits, this should be allowed. The implementation was refactored in 3f8c729. That commit is the first time the "final" check appears AFAICT, but no reason was given for why. That commit seems to have landed in 5.4.11 and the NEWS for that version doesn't seem to mention something relevant to the behaviour change. This patch removes the restriction of the final modifier. Closes GH-11394.
1 parent bde6f2a commit 79d024a

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.0alpha2
44

5+
- Core:
6+
. Fix GH-11388 (Allow "final" modifier when importing a method from a trait).
7+
(nielsdos)
58

69
08 Jun 2023, PHP 8.3.0alpha1
710

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PHP 8.3 UPGRADE NOTES
6363
. Class, interface, trait, and enum constants now support type
6464
declarations. RFC: https://wiki.php.net/rfc/typed_class_constants
6565
. Closures created from magic methods can now accept named arguments.
66+
. The final modifier may now be used when using a method from a trait.
6667

6768
- Posix
6869
. posix_getrlimit() now takes an optional $res parameter to allow fetching a

Zend/tests/traits/language019.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class C1 {
1010
T1::foo as final;
1111
}
1212
}
13+
class C2 extends C1 {
14+
public function foo() {}
15+
}
1316
?>
1417
--EXPECTF--
15-
Fatal error: Cannot use "final" as method modifier in trait alias in %s on line %d
18+
Fatal error: Cannot override final method C1::foo() in %s on line %d

Zend/tests/traits/language020.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
final alias - positive test variation
3+
--FILE--
4+
<?php
5+
trait T1 {
6+
function foo() {
7+
echo "Done\n";
8+
}
9+
}
10+
class C1 {
11+
use T1 {
12+
T1::foo as final;
13+
}
14+
}
15+
class C2 extends C1 {
16+
public function bar() {}
17+
}
18+
(new C2)->foo();
19+
?>
20+
--EXPECT--
21+
Done

Zend/zend_compile.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7727,8 +7727,6 @@ static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
77277727
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
77287728
} else if (attr & ZEND_ACC_ABSTRACT) {
77297729
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
7730-
} else if (attr & ZEND_ACC_FINAL) {
7731-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"final\" as method modifier in trait alias");
77327730
}
77337731
}
77347732
/* }}} */

0 commit comments

Comments
 (0)