PHP PDO: Fetching data as objects - properties assigned BEFORE __construct is called. Is this correc
Posted
by Erik
on Stack Overflow
See other posts from Stack Overflow
or by Erik
Published on 2010-05-19T01:25:19Z
Indexed on
2010/05/19
1:30 UTC
Read the original article
Hit count: 306
The full question should be "Is this correct or is it some bug I can't count on?"
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.
© Stack Overflow or respective owner