Category
language
Problem
#[\Override] on a method that only overrides a trait-provided method (no parent class method in the inheritance chain) must fail at compile time in php-src. Partial coverage exists: trait alias + Override on child is already rejected; direct trait method redefinition in the same class still compiles.
Verified 2026-06-05 on bin/vm.php:
| Scenario |
Zend 8.3+ |
This compiler |
Class use T; #[\Override] public function f() redefines trait method |
Compile-time fatal |
Compiles silently ❌ |
Child overrides trait alias T::f as g with #[\Override] |
Compile-time fatal |
Already rejected ✅ |
php-src reference
Repro
Still broken (direct trait override):
<?php
trait T { public function f(): void {} }
class C {
use T;
#[\Override]
public function f(): void { echo "class\n"; }
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
cat > /tmp/override_trait_direct.php <<'"'"'PHPEOF'"'"'
<?php
trait T { public function f(): void {} }
class C {
use T;
#[\Override]
public function f(): void { echo "class\n"; }
}
PHPEOF
php bin/vm.php /tmp/override_trait_direct.php # compiles — should fatal at compile time
'
Zend expected:
Fatal error: C::f() has #[\Override] attribute, but no matching parent method exists
Already fixed (alias case — regression guard):
trait T { public function f(): void {} }
class C { use T { f as g; } }
class D extends C {
#[\Override]
public function g(): void {}
}
Scope
| Path |
Work |
lib/Compiler/OverrideValidator.php (or lib/Compiler.php) |
Treat trait-origin methods as non-parent for Override target walk |
test/compliance/cases/language/ |
PHPT compile-fail fixtures |
Done when
Strictness
php-src-strict — compile-time parity with Zend attribute semantics.
Related
Category
languageProblem
#[\Override]on a method that only overrides a trait-provided method (no parent class method in the inheritance chain) must fail at compile time in php-src. Partial coverage exists: trait alias + Override on child is already rejected; direct trait method redefinition in the same class still compiles.Verified 2026-06-05 on
bin/vm.php:use T; #[\Override] public function f()redefines trait methodT::f as gwith#[\Override]php-src reference
Zend/zend_compile.c—ZEND_ACC_OVERRIDEvalidation walks the parent class chain; trait-provided methods are not valid override targets (PHP 8.3+)Zend/zend_attributes.c—Overrideattribute handlingRepro
Still broken (direct trait override):
Zend expected:
Already fixed (alias case — regression guard):
Scope
lib/Compiler/OverrideValidator.php(orlib/Compiler.php)test/compliance/cases/language/Done when
echo)test/compliance/cases/language/override_trait_method.phptadded (EXPECT compile failure)Strictness
php-src-strict — compile-time parity with Zend attribute semantics.
Related