note 111744 added to pdostatement.fetchobject

From: Date: Sat, 23 Mar 2013 21:53:58 +0000
Subject: note 111744 added to pdostatement.fetchobject
Groups: php.notes 
Request: Send a blank email to php-notes+get-194185@lists.php.net to get a copy of this message
Be warned of the rather unorthodox behavior of PDOStatement::fetchObject() which injects property-values BEFORE invoking the constructor - in other words, if your class initializes property-values to defaults in the constructor, you will be overwriting the values injected by fetchObject() ! A var_dump($this) in your __construct() method will reveal that property-values have been initialized prior to calling your constructor, so be careful. For this reason, I strongly recommend hydrating your objects manually, after retrieving the data as an array, rather than trying to have PDO apply properties directly to your objects. Clearly somebody thought they were being clever here - allowing you to access hydrated property-values from the constructor. Unfortunately, this is just not how OOP works - the constructor, by definition, is the first method called upon construction. If you need to initialize your objects after they have been constructed and hydrated, I suggest your model types implement an interface with an init() method, and you data access layer invoke this method (if implemented) after hydrating. ---- Server IP: 69.147.83.199 Probable Submitter: 24.59.149.93 ---- Manual Page -- http://php.net/manual/en/pdostatement.fetchobject.php Edit -- https://master.php.net/note/edit/111744 Del: integrated -- https://master.php.net/note/delete/111744/integrated Del: useless -- https://master.php.net/note/delete/111744/useless Del: bad code -- https://master.php.net/note/delete/111744/bad+code Del: spam -- https://master.php.net/note/delete/111744/spam Del: non-english -- https://master.php.net/note/delete/111744/non-english Del: in docs -- https://master.php.net/note/delete/111744/in+docs Del: other reasons-- https://master.php.net/note/delete/111744 Reject -- https://master.php.net/note/reject/111744 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194185) next »