Skip to content

Commit fadab82

Browse files
authored
Missing getAttributes documentation
Closes GH-1567.
1 parent 3e30532 commit fadab82

File tree

6 files changed

+558
-97
lines changed

6 files changed

+558
-97
lines changed

language-snippets.ent

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,34 @@ to be references, then they must be references in the passed argument list.'>
24962496

24972497
<!ENTITY reflection.export.param.name 'The reflection to export.'>
24982498

2499+
<!ENTITY reflection.getattributes.param.name '<varlistentry xmlns="http://docbook.org/ns/docbook">
2500+
<term><parameter>name</parameter></term>
2501+
<listitem>
2502+
<para>
2503+
Filter the results to include only <classname>ReflectionAttribute</classname>
2504+
instances for attributes matching this class name.
2505+
</para>
2506+
</listitem>
2507+
</varlistentry>'>
2508+
2509+
<!ENTITY reflection.getattributes.param.flags '<varlistentry xmlns="http://docbook.org/ns/docbook">
2510+
<term><parameter>flags</parameter></term>
2511+
<listitem>
2512+
<para>
2513+
Flags for determining how to filter the results, if <parameter>name</parameter>
2514+
is provided.
2515+
</para>
2516+
<para>
2517+
Default is <literal>0</literal> which will only return results for attributes that
2518+
are of the class <parameter>name</parameter>.
2519+
</para>
2520+
<para>
2521+
The only other option available, is to use <constant>ReflectionAttribute::IS_INSTANCEOF</constant>,
2522+
which will instead use <literal>instanceof</literal> for filtering.
2523+
</para>
2524+
</listitem>
2525+
</varlistentry>'>
2526+
24992527
<!-- SPL -->
25002528
<!ENTITY spl.datastructures.intro.title '<title xmlns="http://docbook.org/ns/docbook">Datastructures</title>'>
25012529

reference/reflection/reflectionclass/getattributes.xml

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,8 @@
2222
<refsect1 role="parameters">
2323
&reftitle.parameters;
2424
<variablelist>
25-
<varlistentry>
26-
<term><parameter>name</parameter></term>
27-
<listitem>
28-
<para>
29-
Filter the results to include only <classname>ReflectionAttribute</classname>
30-
instances for attributes matching this class name.
31-
</para>
32-
</listitem>
33-
</varlistentry>
34-
<varlistentry>
35-
<term><parameter>flags</parameter></term>
36-
<listitem>
37-
<para>
38-
Flags for determining how to filter the results, if <parameter>name</parameter>
39-
is provided.
40-
</para>
41-
<para>
42-
Default is <literal>0</literal> which will only return results for attributes that
43-
are of the class <parameter>name</parameter>.
44-
</para>
45-
<para>
46-
The only other option available, is to use <constant>ReflectionAttribute::IS_INSTANCEOF</constant>,
47-
which will instead use <literal>instanceof</literal> for filtering.
48-
</para>
49-
</listitem>
50-
</varlistentry>
25+
&reflection.getattributes.param.name;
26+
&reflection.getattributes.param.flags;
5127
</variablelist>
5228
</refsect1>
5329

@@ -137,7 +113,7 @@ Array
137113
<example>
138114
<title>Filtering results by class name, with inheritance</title>
139115
<programlisting role="php">
140-
<![CDATA[
116+
<![CDATA[
141117
<?php
142118
interface Color {
143119
}

reference/reflection/reflectionclassconstant/getattributes.xml

Lines changed: 121 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,8 @@
2121
<refsect1 role="parameters">
2222
&reftitle.parameters;
2323
<variablelist>
24-
<varlistentry>
25-
<term><parameter>name</parameter></term>
26-
<listitem>
27-
<para>
28-
29-
</para>
30-
</listitem>
31-
</varlistentry>
32-
<varlistentry>
33-
<term><parameter>flags</parameter></term>
34-
<listitem>
35-
<para>
36-
37-
</para>
38-
</listitem>
39-
</varlistentry>
24+
&reflection.getattributes.param.name;
25+
&reflection.getattributes.param.flags;
4026
</variablelist>
4127
</refsect1>
4228

@@ -47,6 +33,125 @@
4733
</para>
4834
</refsect1>
4935

36+
<refsect1 role="examples">
37+
&reftitle.examples;
38+
<para>
39+
<example>
40+
<title>Basic usage</title>
41+
<programlisting role="php">
42+
<![CDATA[
43+
<?php
44+
#[Attribute]
45+
class Fruit {
46+
}
47+
48+
#[Attribute]
49+
class Red {
50+
}
51+
52+
class Basket {
53+
#[Fruit]
54+
#[Red]
55+
public const APPLE = 'apple';
56+
}
57+
58+
$classConstant = new ReflectionClassConstant('Basket', 'APPLE');
59+
$attributes = $classConstant->getAttributes();
60+
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
61+
?>
62+
]]>
63+
</programlisting>
64+
&example.outputs;
65+
<screen>
66+
<![CDATA[
67+
Array
68+
(
69+
[0] => Fruit
70+
[1] => Red
71+
)
72+
]]>
73+
</screen>
74+
</example>
75+
</para>
76+
<para>
77+
<example>
78+
<title>Filtering results by class name</title>
79+
<programlisting role="php">
80+
<![CDATA[
81+
<?php
82+
#[Attribute]
83+
class Fruit {
84+
}
85+
86+
#[Attribute]
87+
class Red {
88+
}
89+
90+
class Basket {
91+
#[Fruit]
92+
#[Red]
93+
public const APPLE = 'apple';
94+
}
95+
96+
$classConstant = new ReflectionClassConstant('Basket', 'APPLE');
97+
$attributes = $classConstant->getAttributes('Fruit');
98+
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
99+
?>
100+
]]>
101+
</programlisting>
102+
&example.outputs;
103+
<screen>
104+
<![CDATA[
105+
Array
106+
(
107+
[0] => Fruit
108+
)
109+
]]>
110+
</screen>
111+
</example>
112+
</para>
113+
<para>
114+
<example>
115+
<title>Filtering results by class name, with inheritance</title>
116+
<programlisting role="php">
117+
<![CDATA[
118+
<?php
119+
interface Color {
120+
}
121+
122+
#[Attribute]
123+
class Fruit {
124+
}
125+
126+
#[Attribute]
127+
class Red implements Color {
128+
}
129+
130+
class Basket {
131+
#[Fruit]
132+
#[Red]
133+
public const APPLE = 'apple';
134+
}
135+
136+
$classConstant = new ReflectionClassConstant('Basket', 'APPLE');
137+
$attributes = $classConstant->getAttributes('Color', ReflectionAttribute::IS_INSTANCEOF);
138+
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
139+
?>
140+
]]>
141+
</programlisting>
142+
&example.outputs;
143+
<screen>
144+
<![CDATA[
145+
Array
146+
(
147+
[0] => Red
148+
)
149+
]]>
150+
</screen>
151+
</example>
152+
</para>
153+
</refsect1>
154+
50155
<refsect1 role="seealso">
51156
&reftitle.seealso;
52157
<para>

0 commit comments

Comments
 (0)