1--TEST-- 2Attributes make use of class scope. 3--FILE-- 4<?php 5 6#[A1(self::class, self::FOO)] 7class C1 8{ 9 #[A1(self::class, self::FOO)] 10 private const FOO = 'foo'; 11 12 #[A1(self::class, self::FOO)] 13 public $a; 14 15 #[A1(self::class, self::FOO)] 16 public function bar(#[A1(self::class, self::FOO)] $p) { } 17} 18 19$ref = new \ReflectionClass(C1::class); 20print_r($ref->getAttributes()[0]->getArguments()); 21print_r($ref->getReflectionConstant('FOO')->getAttributes()[0]->getArguments()); 22print_r($ref->getProperty('a')->getAttributes()[0]->getArguments()); 23print_r($ref->getMethod('bar')->getAttributes()[0]->getArguments()); 24print_r($ref->getMethod('bar')->getParameters()[0]->getAttributes()[0]->getArguments()); 25 26echo "\n"; 27 28trait T1 29{ 30 #[A1(self::class, self::FOO)] 31 public function foo() { } 32} 33 34class C2 35{ 36 use T1; 37 38 private const FOO = 'bar'; 39} 40 41$ref = new \ReflectionClass(C2::class); 42print_r($ref->getMethod('foo')->getAttributes()[0]->getArguments()); 43 44$ref = new \ReflectionClass(T1::class); 45$attr = $ref->getMethod('foo')->getAttributes()[0]; 46 47try { 48 $attr->getArguments(); 49} catch (\Error $e) { 50 var_dump('ERROR 1', $e->getMessage()); 51} 52 53echo "\n"; 54 55class C3 56{ 57 private const FOO = 'foo'; 58 59 public static function foo() 60 { 61 return new #[A1(self::class, self::FOO)] class() { 62 private const FOO = 'bar'; 63 64 #[A1(self::class, self::FOO)] 65 public function bar() { } 66 }; 67 } 68} 69 70$ref = new \ReflectionObject(C3::foo()); 71 72$args = $ref->getAttributes()[0]->getArguments(); 73var_dump($args[0] == $ref->getName(), $args[1]); 74 75$args = $ref->getMethod('bar')->getAttributes()[0]->getArguments(); 76var_dump($args[0] == $ref->getName(), $args[1]); 77 78?> 79--EXPECT-- 80Array 81( 82 [0] => C1 83 [1] => foo 84) 85Array 86( 87 [0] => C1 88 [1] => foo 89) 90Array 91( 92 [0] => C1 93 [1] => foo 94) 95Array 96( 97 [0] => C1 98 [1] => foo 99) 100Array 101( 102 [0] => C1 103 [1] => foo 104) 105 106Array 107( 108 [0] => C2 109 [1] => bar 110) 111string(7) "ERROR 1" 112string(28) "Undefined constant self::FOO" 113 114bool(true) 115string(3) "bar" 116bool(true) 117string(3) "bar" 118