©
本文档使用 PHP中文网手册 发布
(PHP 5 >= 5.1.0, PECL pdo >= 0.2.4)
PDOStatement::fetchObject — 获取下一行并作为一个对象返回。
$class_name
= "stdClass"
[, array $ctor_args
]] )
获取下一行并作为一个对象返回。此函数(方法)是使用 PDO::FETCH_CLASS
或 PDO::FETCH_OBJ
风格的 PDOStatement::fetch() 的一种替代。
class_name
创建类的名称。
ctor_args
此数组的元素被传递给构造函数。
返回一个属性名对应于列名的所要求类的实例, 或者在失败时返回 FALSE
.
[#1] beinghavingbreackfast at gmail dot com [2015-07-29 20:51:02]
It should be mentioned that this method can set even non-public properties. It may sound strange but it can actually be very useful when creating an object based on mysql result.
Consider a User class:
<?php
class User {
// Private properties
private $id, $name;
private function __construct () {}
public static function load_by_id ($id) {
$stmt = $pdo->prepare('SELECT id, name FROM users WHERE id=?');
$stmt->execute([$id]);
return $stmt->fetchObject(__CLASS__);
}
}
$user = User::load_by_id(1);
var_dump($user);
?>
fetchObject() doesn't care about properties being public or not. It just passes the result to the object. Output is like:
object(User)#3 (2) {
["id":"User":private]=>
string(1) "1"
["name":"User":private]=>
string(10) "John Smith"
}
[#2] dave at davidhbrown dot us [2015-06-27 03:03:42]
If using a namespaced class, you must provide the fully qualified class name; fetchObject does not seem to know about any "use" statements.
This results in a PHP Fatal error: Class 'MyClass' not found...:
<?php
use MyNamespace\MyClass;
// ...
$o = $statement->fetchObject('MyClass'));
?>
This works:
<?php
use MyNamespace\MyClass; //still needed for my code
// ...
$o = $statement->fetchObject('MyNamespace\\MyClass'));
?>
[#3] Anonymous [2015-03-15 23:34:37]
I think so could us use this variant to implement the constructor, this is my example:
<?php
class User{
public $user;
public $password;
public $name;
public $email;
public function __construct(){
$args = func_get_args();
$nargs = func_num_args();
$attribs = get_class_vars(get_class($this));
if(isset($args)){
foreach($args as $value){
$attrib = key($attribs);
$this->$attrib = $value;
next($attribs);
}
}
}
}
[#4] Vegard Lkken [2014-03-18 07:19:44]
Because of the injection of object properties before the constructor is executed, I usually build my classes like this to make sure already set properties aren't overwritten:
<?php
class Person {
public $name;
public $age;
public $sex;
public function __construct($name=NULL, $age=NULL, $sex=NULL) {
$this->name = $name === NULL ? $this->name : $name;
$this->age = $age === NULL ? $this->age : $age;
$this->sex = $sex === NULL ? $this->sex : $sex;
}
}
?>
[#5] rasmus at mindplay dot dk [2013-03-23 21:53:56]
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.