Skip to content

Commit

Permalink
Allow final modifier when using a method from a trait (#11394)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nielsdos committed Jun 7, 2023
1 parent bde6f2a commit 79d024a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.3.0alpha2

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

08 Jun 2023, PHP 8.3.0alpha1

Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Expand Up @@ -63,6 +63,7 @@ PHP 8.3 UPGRADE NOTES
. Class, interface, trait, and enum constants now support type
declarations. RFC: https://wiki.php.net/rfc/typed_class_constants
. Closures created from magic methods can now accept named arguments.
. The final modifier may now be used when using a method from a trait.

- Posix
. posix_getrlimit() now takes an optional $res parameter to allow fetching a
Expand Down
5 changes: 4 additions & 1 deletion Zend/tests/traits/language019.phpt
Expand Up @@ -10,6 +10,9 @@ class C1 {
T1::foo as final;
}
}
class C2 extends C1 {
public function foo() {}
}
?>
--EXPECTF--
Fatal error: Cannot use "final" as method modifier in trait alias in %s on line %d
Fatal error: Cannot override final method C1::foo() in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/traits/language020.phpt
@@ -0,0 +1,21 @@
--TEST--
final alias - positive test variation
--FILE--
<?php
trait T1 {
function foo() {
echo "Done\n";
}
}
class C1 {
use T1 {
T1::foo as final;
}
}
class C2 extends C1 {
public function bar() {}
}
(new C2)->foo();
?>
--EXPECT--
Done
2 changes: 0 additions & 2 deletions Zend/zend_compile.c
Expand Up @@ -7727,8 +7727,6 @@ static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
} else if (attr & ZEND_ACC_ABSTRACT) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
} else if (attr & ZEND_ACC_FINAL) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"final\" as method modifier in trait alias");
}
}
/* }}} */
Expand Down

0 comments on commit 79d024a

Please sign in to comment.