summaryrefslogtreecommitdiff
path: root/Zend/tests/attributes/013_class_scope.phpt
blob: ff16bb7b822585b83b80108904aeba213081b14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--TEST--
Attributes make use of class scope.
--FILE--
<?php

#[A1(self::class, self::FOO)]
class C1
{
    #[A1(self::class, self::FOO)]
    private const FOO = 'foo';

    #[A1(self::class, self::FOO)]
    public $a;

    #[A1(self::class, self::FOO)]
    public function bar(#[A1(self::class, self::FOO)] $p) { }
}

$ref = new \ReflectionClass(C1::class);
print_r($ref->getAttributes()[0]->getArguments());
print_r($ref->getReflectionConstant('FOO')->getAttributes()[0]->getArguments());
print_r($ref->getProperty('a')->getAttributes()[0]->getArguments());
print_r($ref->getMethod('bar')->getAttributes()[0]->getArguments());
print_r($ref->getMethod('bar')->getParameters()[0]->getAttributes()[0]->getArguments());

echo "\n";

trait T1
{
    #[A1(self::class, self::FOO)]
    public function foo() { }
}

class C2
{
    use T1;

    private const FOO = 'bar';
}

$ref = new \ReflectionClass(C2::class);
print_r($ref->getMethod('foo')->getAttributes()[0]->getArguments());

$ref = new \ReflectionClass(T1::class);
$attr = $ref->getMethod('foo')->getAttributes()[0];

try {
    $attr->getArguments();
} catch (\Error $e) {
    var_dump('ERROR 1', $e->getMessage());
}

echo "\n";

class C3
{
    private const FOO = 'foo';

    public static function foo()
    {
        return new #[A1(self::class, self::FOO)] class() {
            private const FOO = 'bar';

            #[A1(self::class, self::FOO)]
            public function bar() { }
        };
    }
}

$ref = new \ReflectionObject(C3::foo());

$args = $ref->getAttributes()[0]->getArguments();
var_dump($args[0] == $ref->getName(), $args[1]);

$args = $ref->getMethod('bar')->getAttributes()[0]->getArguments();
var_dump($args[0] == $ref->getName(), $args[1]);

?>
--EXPECT--
Array
(
    [0] => C1
    [1] => foo
)
Array
(
    [0] => C1
    [1] => foo
)
Array
(
    [0] => C1
    [1] => foo
)
Array
(
    [0] => C1
    [1] => foo
)
Array
(
    [0] => C1
    [1] => foo
)

Array
(
    [0] => C2
    [1] => bar
)
string(7) "ERROR 1"
string(28) "Undefined constant self::FOO"

bool(true)
string(3) "bar"
bool(true)
string(3) "bar"