Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO: Fetching data as objects - properties assigned BEFORE __construct is called. Is this correct?

Tags:

php

pdo

The full question should be "Is this correct or is it some bug I can't count on?"

WHY is this correct behavior?

I've been working with PDO more and in particular playing with fetching data directly into objects. While doing so I discovered this:

If I fetch data directly into an object like this:

$STH = $DBH->prepare('SELECT first_name, address from people WHERE 1');
$obj = $STH->fetchAll(PDO::FETCH_CLASS, 'person');

and have an object like this:

class person {
  public $first_name;
  public $address;

  function __construct() {
    $this->first_name = $this->first_name . " is the name";
  }
}

It shows me that the properties are being assigned before the __construct is being called -- because the names all have " is the name" appended.

Is this some bug (in which case I can't/won't count on it) or is this The Way It Should Be. Because it's really quite a nice thing the way it works currently.

Update

Apparently, according to one of the maintainers this is not a bug. Someone posted it as a bug in 2008, to which the reply was 'its not a bug, read the docs'.

However, I'd really like to know WHY this is correct behavior.

like image 627
Erik Avatar asked May 19 '10 01:05

Erik


3 Answers

After much reading I think I finally came across the answer: it works this way intentionally, and you have the option of making it operate otherwise.

There's a mostly undocumented PDO constant called PDO::FETCH_PROPS_LATE which you can use to cause the properties to be fetched into the object after its been constructed. For example:

$obj = $STH->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'person');

Will cause the properties to be assigned AFTER the object is created, so my example above would not modify the properties at all. Leaving the PDO::FETCH_PROPS_LATE off, of course, causes it to act as described in my example in the original question.

The maintainers seem to have actively considered that both behaviors may be desirable and given you the option to do either. The documentation doesn't even explain it -- I was reading through a list of PDO constants and saw it, and gave it a shot.

like image 54
Erik Avatar answered Nov 19 '22 07:11

Erik


The reason is, when you serialize an object, either to a database or a string, you don't (normally) want to reinitialise the properties when unserializing it.

like image 3
Petah Avatar answered Nov 19 '22 07:11

Petah


Try using PDO::FETCH_INTO instead of PDO::FETCH_CLASS. From the docs:

PDO::FETCH_INTO: updates an existing instance of the requested class, mapping the columns of the result set to named properties in the class

So, you'd create the instance first, then pass the instance to your desired fetch method.

That being said, yeah it is rather counter-intuitive to have FETCH_CLASS populate before calling __construct. The answer given on the mailing list is the standard copy-and-paste "RTM" answer. If FETCH_INTO works, you should open a documentation bug with the suggested enhancement(s).

like image 2
Charles Avatar answered Nov 19 '22 08:11

Charles