I have a class that uses magic methods to store properties. Here is a simplified example:
class Foo {
protected $props;
public function __construct(array $props = array()) {
$this->props = $props;
}
public function __get($prop) {
return $this->props[$prop];
}
public function __set($prop, $val) {
$this->props[$prop] = $val;
}
}
I'm trying to instantiate objects of this class for each database row of a PDOStatement
after it's executed, like this (doesn't work):
$st->setFetchMode(PDO::FETCH_CLASS, 'Foo');
foreach ($st as $row) {
var_dump($row);
}
The problem is that PDO::FETCH_CLASS
does not seem to trigger the magic __set()
method on my class when it's setting property values.
How can I accomplish the desired effect using PDO?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…