Php PDO::FETCH_类,如何编写构造函数参数?
我喜欢使用PDO::FETCH_类来获取对象数组,应该调用构造函数,因为我需要这个类中的构造函数。作为第三个参数,我使用了数据库表中列的名称。结果,我得到了列的名称,但不是数据库中的预期值。 我和mariaDB一起工作 没有构造函数,只有2个参数,我就得到了一个包含正确对象的数组Php PDO::FETCH_类,如何编写构造函数参数?,php,mysql,pdo,Php,Mysql,Pdo,我喜欢使用PDO::FETCH_类来获取对象数组,应该调用构造函数,因为我需要这个类中的构造函数。作为第三个参数,我使用了数据库表中列的名称。结果,我得到了列的名称,但不是数据库中的预期值。 我和mariaDB一起工作 没有构造函数,只有2个参数,我就得到了一个包含正确对象的数组 $dbh = Db::connect(); $sql = 'SELECT * FROM dancer'; $stmt = $dbh->prepare($sql); $stmt->exec
$dbh = Db::connect();
$sql = 'SELECT * FROM dancer';
$stmt = $dbh->prepare($sql);
$stmt->execute();
// additional "|PDO::FETCH_PROPS_LATE" in 1. argument ignores
// the constructor
$dancers = $stmt->fetchAll(PDO::FETCH_CLASS
,"Dancer"
,array("firstName","lastName","id")
);
预期结果:
实际结果:
PHP类
自从我研究使用FETCH_类以来已经有很长一段时间了,所以我花了一段时间才想起它是如何结合在一起的。我做了一个测试:
class foo{
private $username;
private $userid;
public function __construct( $arg1, $arg2 ){
/* assign "ctor_args" as named properties */
$this->username=$this->$arg1;
$this->userid=$this->$arg2;
/* do something with the newly created properties */
$this->render( $arg1, $arg2 );
}
private function render( $arg1, $arg2 ){
printf('
<pre>
username: %s
userid: %s
</pre>',
$this->username,
$this->userid
);
}
}
$ctor_args=array( 'name', 'uid' );
/*
use aliases to modify column names to
indicate the assignment within the class
later
*/
$sql='select `username` as `name`, `userid` as `uid` from users';
$stmt=$db->prepare( $sql );
$res=$stmt->execute();
/* fetch results - calling the `foo` class with additional `ctor_args` */
$stmt->fetchAll( PDO::FETCH_CLASS, 'foo', $ctor_args );
因此,如果您将构造函数方法更改为
PDO::FETCH_类:返回请求类的新实例,将结果集的列映射到类中的命名属性,然后调用构造函数,除非还提供了PDO::FETCH_PROPS_LATE也许最好将舞者类定义添加到你为什么评论FETCH_PROPS_晚了?
[0] => Dancer Object
(
[id:Dancer:private] => id
[firstName:Dancer:private] => firstName
[lastName:Dancer:private] => lastName
)
class Dancer
{
private $id;
private $firstName;
private $lastName;
public function __construct($firstName, $lastName, $id = null)
{
if (isset($id)){
$this->id = $id;
}
$this->firstName = $firstName;
$this->lastName = $lastName;
}
#GETTER AND SETTER
public static function getAll()
{
try {
$dbh = Db::connect();
$sql = 'SELECT * FROM dancer';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$dancers = $stmt->fetchAll(PDO::FETCH_CLASS
,"Dancer"
,array("firstName","lastName","id")
);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
return $dancers;
}
}
class foo{
private $username;
private $userid;
public function __construct( $arg1, $arg2 ){
/* assign "ctor_args" as named properties */
$this->username=$this->$arg1;
$this->userid=$this->$arg2;
/* do something with the newly created properties */
$this->render( $arg1, $arg2 );
}
private function render( $arg1, $arg2 ){
printf('
<pre>
username: %s
userid: %s
</pre>',
$this->username,
$this->userid
);
}
}
$ctor_args=array( 'name', 'uid' );
/*
use aliases to modify column names to
indicate the assignment within the class
later
*/
$sql='select `username` as `name`, `userid` as `uid` from users';
$stmt=$db->prepare( $sql );
$res=$stmt->execute();
/* fetch results - calling the `foo` class with additional `ctor_args` */
$stmt->fetchAll( PDO::FETCH_CLASS, 'foo', $ctor_args );
username: RamRaider
userid: d6ba0682d75eb986237fb6b594f8a31f
username: Dustybin
userid: FHsdf44dfsdfcvhbfgh86237fb6b594f8a31f
public function __construct($firstName, $lastName, $id = null){
if( isset( $this->$id ) ){
$this->id = $this->$id;
}
$this->firstName = $this->$firstName;
$this->lastName = $this->$lastName;
}