[php-src] Issue #19153: Attribute validation should error on trait/interface/enum/abstract class
| From: | DanielEScherzer | Date: | Wed, 16 Jul 2025 22:15:48 +0000 |
| Subject: | [php-src] Issue #19153: Attribute validation should error on trait/interface/enum/abstract class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250445@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/19153
Author: DanielEScherzer
### Description
See https://3v4l.org/99PhY
```php
<?php
#[Attribute]
enum DemoEnum {}
#[Attribute]
trait DemoTrait {}
#[Attribute]
abstract class DemoAbstract {}
#[Attribute]
interface DemoInterface {}
#[DemoEnum]
#[DemoTrait]
#[DemoAbstract]
#[DemoInterface]
function usesWrongAttrib() {}
$r = new ReflectionFunction( 'usesWrongAttrib' );
$a = $r->getAttributes();
var_dump( $a );
foreach ( $a as $attrib ) {
try {
$attrib->newInstance();
} catch ( Error $e ) {
echo get_class( $e ) . ": " . $e->getMessage() . "\n";
}
}
```
currently outputs
```
array(4) {
[0]=>
object(ReflectionAttribute)#2 (1) {
["name"]=>
string(8) "DemoEnum"
}
[1]=>
object(ReflectionAttribute)#3 (1) {
["name"]=>
string(9) "DemoTrait"
}
[2]=>
object(ReflectionAttribute)#4 (1) {
["name"]=>
string(12) "DemoAbstract"
}
[3]=>
object(ReflectionAttribute)#5 (1) {
["name"]=>
string(13) "DemoInterface"
}
}
Error: Cannot instantiate enum DemoEnum
Error: Cannot instantiate trait DemoTrait
Error: Cannot instantiate abstract class DemoAbstract
Error: Cannot instantiate interface DemoInterface
```
But we should have compile-time errors instead.