Skip to content

Commit a8863b0

Browse files
pereorgaGirgiaskamil-tekiela
authored
Clarify how properties are populated when using PDO::FETCH_CLASS (#4360)
Private and read-only properties are populated and mention PDO::FETCH_PROPS_LATE consistently. Co-authored-by: Gina Peter Banyard <[email protected]> Co-authored-by: Kamil Tekiela <[email protected]>
1 parent 8c80c20 commit a8863b0

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

reference/pdo/constants.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,20 @@
242242
<listitem>
243243
<simpara>
244244
Specifies that the fetch method shall return a new instance of the
245-
requested class, mapping the columns to named properties in the class.
245+
requested class.
246246
</simpara>
247247
<note>
248248
<simpara>
249-
The magic
250-
<link linkend="language.oop5.overloading.members"><methodname>__set</methodname></link>
251-
method is called if the property doesn't exist in the requested class
249+
The object is initialized by mapping the columns from the result set to
250+
properties in the class. This process occurs before the constructor is
251+
called, allowing the population of properties regardless of their
252+
visibility or whether they are marked as <literal>readonly</literal>. If
253+
a property does not exist in the class, the magic
254+
<link linkend="object.set">__set()</link>
255+
method will be invoked if it exists; otherwise, a dynamic public property
256+
will be created. However, when <constant>PDO::FETCH_PROPS_LATE</constant>
257+
is also given, the constructor is called <emphasis>before</emphasis> the
258+
properties are populated.
252259
</simpara>
253260
</note>
254261
</listitem>

reference/pdo/pdostatement/fetch.xml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@
5353
</para></listitem>
5454
<listitem><para>
5555
<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.
6368
</para></listitem>
6469
<listitem><para>
6570
<literal>PDO::FETCH_INTO</literal>: updates an existing instance
@@ -266,11 +271,11 @@ Reading backwards:
266271

267272
<example><title>Construction order</title>
268273
<simpara>
269-
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
270275
properties are assigned first, and then the constructor of the class is
271-
invoked. If <literal>PDO::FETCH_PROPS_LATE</literal> is also given, this
272-
order is reversed, i.e. first the constructor is called, and afterwards the
273-
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.
274279
</simpara>
275280
<programlisting role="php">
276281
<![CDATA[

0 commit comments

Comments
 (0)