pdostatement::fetch-k8凯发旗舰
当通过 pdo::fetch_class
获取对象时,首先分配对象属性,然后调用类的构造方法。如果还指定了 pdo::fetch_props_late
,则此顺序相反,即首先调用构造方法,然后分配属性。
class person
{
private $name;
public function __construct()
{
$this->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();
?>
以上例程的输出类似于:
i am alice. i am alice. i don't have a name yet. i am bob.#pdo #php