From e440c79af15cf829549736b2a292b5f8f6887142 Mon Sep 17 00:00:00 2001 From: Christoph Michael Becker Date: Wed, 29 Jun 2016 11:03:39 +0000 Subject: [PATCH] Fix #53394: Insufficient docs regarding PDOStatement::fetch(Object) and constructors git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@339520 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/pdo/pdostatement/fetch.xml | 60 +++++++++++++++++++++- reference/pdo/pdostatement/fetchobject.xml | 4 ++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/reference/pdo/pdostatement/fetch.xml b/reference/pdo/pdostatement/fetch.xml index 4624f99b14..85a6829a47 100644 --- a/reference/pdo/pdostatement/fetch.xml +++ b/reference/pdo/pdostatement/fetch.xml @@ -54,7 +54,9 @@ PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named - properties in the class. If fetch_style + properties in the class, and calling the constructor afterwards, unless + PDO::FETCH_PROPS_LATE is also given. + If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column. @@ -85,6 +87,12 @@ property names that correspond to the column names returned in your result set + + PDO::FETCH_PROPS_LATE: when used with + PDO::FETCH_CLASS, the constructor of the class is + called before the properties are assigned from the respective column + values. + @@ -262,6 +270,56 @@ Reading backwards: + Construction order + + When objects are fetched via PDO::FETCH_CLASS the object + properties are assigned first, and then the constructor of the class is + invoked. If PDO::FETCH_PROPS_LATE is also given, this + order is reversed, i.e. first the constructor is called, and afterwards the + properties are assigned. + + +tell(); + } + + public function tell() + { + if (isset($this->name)) { + echo "I am {$this->name}.\n"; + } else { + echo "I don't have a name yet.\n"; + } + } +} + +$sth = $dbh->query("SELECT * FROM people"); +$sth->setFetchMode(PDO::FETCH_CLASS, 'Person'); +$person = $sth->fetch(); +$person->tell(); +$sth->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Person'); +$person = $sth->fetch(); +$person->tell(); +?> +]]> + + &example.outputs.similar; + + + + diff --git a/reference/pdo/pdostatement/fetchobject.xml b/reference/pdo/pdostatement/fetchobject.xml index 754b223bfe..1f29f40125 100644 --- a/reference/pdo/pdostatement/fetchobject.xml +++ b/reference/pdo/pdostatement/fetchobject.xml @@ -19,6 +19,10 @@ PDO::FETCH_CLASS or PDO::FETCH_OBJ style. + + When an object is fetched, its properties are assigned from respective + column values, and afterwards its constructor is invoked. +