reference/reflection/reflectionproperty/setvalue.xml
0a8a502764676759e213265a3e1e675c43533dd1
...
...
@@ -9,7 +9,7 @@
9
9
10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
-
<methodsynopsis>
12
+
<methodsynopsis role="ReflectionProperty">
13
13
<modifier>public</modifier> <type>void</type><methodname>ReflectionProperty::setValue</methodname>
14
14
<methodparam><type>object</type><parameter>object</parameter></methodparam>
15
15
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
...
...
@@ -21,6 +21,11 @@
21
21
<para>
22
22
Sets (changes) the property's value.
23
23
</para>
24
+
<note>
25
+
<simpara>
26
+
As of PHP 8.3.0, calling this method with a single argument is deprecated, use <methodname>ReflectionClass::setStaticPropertyValue</methodname> instead.
27
+
</simpara>
28
+
</note>
24
29
</refsect1>
25
30
26
31
<refsect1 role="parameters">
...
...
@@ -32,8 +37,8 @@
32
37
<listitem>
33
38
<para>
34
39
If the property is non-static an object must be provided to change
35
-
the property on. If the property is static this parameter is left
36
-
out and only <parameter>value</parameter> needs to be provided.
40
+
the property on.
41
+
If the property is static a value of &null; <emphasis>must</emphasis> be to be provided.
37
42
</para>
38
43
</listitem>
39
44
</varlistentry>
...
...
@@ -56,13 +61,38 @@
56
61
</para>
57
62
</refsect1>
58
63
59
-
<refsect1 role="errors">
60
-
&reftitle.errors;
61
-
<para>
62
-
Throws a <classname>ReflectionException</classname> if the property is inaccessible.
63
-
You can make a protected or private property accessible using
64
-
<methodname>ReflectionProperty::setAccessible</methodname>.
65
-
</para>
64
+
<refsect1 role="changelog">
65
+
&reftitle.changelog;
66
+
<informaltable>
67
+
<tgroup cols="2">
68
+
<thead>
69
+
<row>
70
+
<entry>&Version;</entry>
71
+
<entry>&Description;</entry>
72
+
</row>
73
+
</thead>
74
+
<tbody>
75
+
<row>
76
+
<entry>8.3.0</entry>
77
+
<entry>
78
+
Calling this method with a single argument is deprecated,
79
+
<methodname>ReflectionClass::setStaticPropertyValue</methodname>
80
+
should be used instead to modify static properties.
81
+
</entry>
82
+
</row>
83
+
<row>
84
+
<entry>8.1.0</entry>
85
+
<entry>
86
+
Private and protected properties can be accessed by
87
+
<methodname>ReflectionProperty::setValue</methodname> right away.
88
+
Previously, they needed to be made accessible by calling
89
+
<methodname>ReflectionProperty::setAccessible</methodname>; otherwise
90
+
a <classname>ReflectionException</classname> was thrown.
91
+
</entry>
92
+
</row>
93
+
</tbody>
94
+
</tgroup>
95
+
</informaltable>
66
96
</refsect1>
67
97

68
98
<refsect1 role="examples">
...
...
@@ -82,7 +112,8 @@ class Foo {
82
112

83
113
$reflectionClass = new ReflectionClass('Foo');
84
114

85
-
$reflectionClass->getProperty('staticProperty')->setValue('foo');
115
+
// As of PHP 8.3, setValue should no longer be used to set static property value, use setStaticPropertyValue() instead
116
+
$reflectionClass->setStaticPropertyValue('staticProperty', 'foo');
86
117
var_dump(Foo::$staticProperty);
87
118

88
119
$foo = new Foo;
...
...
@@ -91,7 +122,7 @@ $reflectionClass->getProperty('property')->setValue($foo, 'bar');
91
122
var_dump($foo->property);
92
123

93
124
$reflectionProperty = $reflectionClass->getProperty('privateProperty');
94
-
$reflectionProperty->setAccessible(true);
125
+
$reflectionProperty->setAccessible(true); // only required prior to PHP 8.1.0
95
126
$reflectionProperty->setValue($foo, 'foobar');
96
127
var_dump($reflectionProperty->getValue($foo));
97
128
?>
98
129