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.
This commit is contained in:
Niels Dossche 2023-06-07 23:53:21 +02:00 committed by GitHub
parent bde6f2a2f7
commit 79d024ac0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 3 deletions

3
NEWS
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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");
}
}
/* }}} */