reference/pdo/pdostatement/fetch.xml
a8863b0123dc5348c91e67198b995222878a7e65
...
...
@@ -53,13 +53,18 @@
53
53
</para></listitem>
54
54
<listitem><para>
55
55
<literal>PDO::FETCH_CLASS</literal>: returns a new instance of the
56
-
requested class, mapping the columns of the result set to named
57
-
properties in the class, and calling the constructor afterwards, unless
58
-
<literal>PDO::FETCH_PROPS_LATE</literal> is also given.
59
-
If <parameter>mode</parameter>
60
-
includes PDO::FETCH_CLASSTYPE (e.g. <literal>PDO::FETCH_CLASS |
61
-
PDO::FETCH_CLASSTYPE</literal>) then the name of the class is
62
-
determined from a value of the first column.
56
+
requested class. The object is initialized by mapping the columns of
57
+
the result set to properties in the class. This occurs before the
58
+
constructor is called, allowing properties to be populated regardless
59
+
of their visibility or whether they are marked as <literal>readonly</literal>.
60
+
If a property does not exist in the class, the magic <link linkend="object.set">__set()</link>
61
+
method will be invoked if it exists; otherwise, a dynamic public
62
+
property will be created. However, when <constant>PDO::FETCH_PROPS_LATE</constant>
63
+
is also given, the constructor is called <emphasis>before</emphasis>
64
+
the properties are populated. If <parameter>mode</parameter> includes
65
+
<constant>PDO::FETCH_CLASSTYPE</constant> (e.g.
66
+
<literal>PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE</literal>), the name
67
+
of the class is determined from the value of the first column.
63
68
</para></listitem>
64
69
<listitem><para>
65
70
<literal>PDO::FETCH_INTO</literal>: updates an existing instance
...
...
@@ -69,7 +74,8 @@
69
74
<listitem><para>
70
75
<literal>PDO::FETCH_LAZY</literal>: combines
71
76
<literal>PDO::FETCH_BOTH</literal> and <literal>PDO::FETCH_OBJ</literal>,
72
-
creating the object variable names as they are accessed
77
+
and is returning a <classname>PDORow</classname> object
78
+
which is creating the object property names as they are accessed.
73
79
</para></listitem>
74
80
<listitem><para>
75
81
<literal>PDO::FETCH_NAMED</literal>: returns an array with the same
...
...
@@ -158,29 +164,29 @@ $sth = $dbh->prepare("SELECT name, colour FROM fruit");
158
164
$sth->execute();
159
165

160
166
/* Exercise PDOStatement::fetch styles */
161
-
print("PDO::FETCH_ASSOC: ");
162
-
print("Return next row as an array indexed by column name\n");
167
+
print "PDO::FETCH_ASSOC: ";
168
+
print "Return next row as an array indexed by column name\n";
163
169
$result = $sth->fetch(PDO::FETCH_ASSOC);
164
170
print_r($result);
165
-
print("\n");
171
+
print "\n";
166
172

167
-
print("PDO::FETCH_BOTH: ");
168
-
print("Return next row as an array indexed by both column name and number\n");
173
+
print "PDO::FETCH_BOTH: ";
174
+
print "Return next row as an array indexed by both column name and number\n";
169
175
$result = $sth->fetch(PDO::FETCH_BOTH);
170
176
print_r($result);
171
-
print("\n");
177
+
print "\n";
172
178

173
-
print("PDO::FETCH_LAZY: ");
174
-
print("Return next row as an anonymous object with column names as properties\n");
179
+
print "PDO::FETCH_LAZY: ";
180
+
print "Return next row as a PDORow object with column names as properties\n";
175
181
$result = $sth->fetch(PDO::FETCH_LAZY);
176
182
print_r($result);
177
-
print("\n");
183
+
print "\n";
178
184

179
-
print("PDO::FETCH_OBJ: ");
180
-
print("Return next row as an anonymous object with column names as properties\n");
185
+
print "PDO::FETCH_OBJ: ";
186
+
print "Return next row as an anonymous object with column names as properties\n";
181
187
$result = $sth->fetch(PDO::FETCH_OBJ);
182
188
print $result->name;
183
-
print("\n");
189
+
print "\n";
184
190
?>
185
191
]]>
186
192
</programlisting>
...
...
@@ -203,7 +209,7 @@ Array
203
209
[1] => yellow
204
210
)
205
211

206
-
PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties
212
+
PDO::FETCH_LAZY: Return next row as a PDORow object with column names as properties
207
213
PDORow Object
208
214
(
209
215
[name] => orange
...
...
@@ -265,11 +271,11 @@ Reading backwards:
265
271

266
272
<example><title>Construction order</title>
267
273
<simpara>
268
-
When objects are fetched via <literal>PDO::FETCH_CLASS</literal> the object
274
+
When objects are fetched via <constant>PDO::FETCH_CLASS</constant>, the object
269
275
properties are assigned first, and then the constructor of the class is
270
-
invoked. If <literal>PDO::FETCH_PROPS_LATE</literal> is also given, this
271
-
order is reversed, i.e. first the constructor is called, and afterwards the
272
-
properties are assigned.
276
+
invoked. However, when <constant>PDO::FETCH_PROPS_LATE</constant> is also given,
277
+
this order is reversed, i.e. first the constructor is called, and
278
+
afterwards the properties are assigned.
273
279
</simpara>
274
280
<programlisting role="php">
275
281
<![CDATA[
276
282