1--TEST-- 2Allow defining closures in attributes 3--EXTENSIONS-- 4reflection 5--FILE-- 6<?php 7 8#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] 9class Attr { 10 public function __construct(public Closure $value) { 11 $value('foo'); 12 } 13} 14 15#[Attr(static function () { })] 16#[Attr(static function (...$args) { 17 var_dump($args); 18})] 19class C {} 20 21foreach ((new ReflectionClass(C::class))->getAttributes() as $reflectionAttribute) { 22 var_dump($reflectionAttribute->newInstance()); 23} 24 25?> 26--EXPECTF-- 27object(Attr)#%d (1) { 28 ["value"]=> 29 object(Closure)#%d (3) { 30 ["name"]=> 31 string(%d) "{closure:%s:%d}" 32 ["file"]=> 33 string(%d) "%s" 34 ["line"]=> 35 int(%d) 36 } 37} 38array(1) { 39 [0]=> 40 string(3) "foo" 41} 42object(Attr)#%d (1) { 43 ["value"]=> 44 object(Closure)#%d (4) { 45 ["name"]=> 46 string(%d) "{closure:%s:%d}" 47 ["file"]=> 48 string(%d) "%s" 49 ["line"]=> 50 int(%d) 51 ["parameter"]=> 52 array(1) { 53 ["$args"]=> 54 string(10) "<optional>" 55 } 56 } 57} 58