gre*_*emo 3 php constructor pdo
我正在尝试使用PHP PDO 创建一个新的Setting
对象调用__construct()
方法实例并约束PDO :: FETCH_PROPS_LATE.不幸的是,我收到了这个警告(并且绑定不起作用).
如何将列值传递给构造函数方法?
警告:在pdo.php中缺少Setting :: __ construct()的参数1.
注意:未定义的变量:pdo.php中的键.
class Setting
{
protected $key, $value, $displayable;
public function __construct($key, $value = null, $displayable = 1)
{
$this->key = $key;
$this->value = $value;
$this->displayable = $displayable > 0;
}
}
while($mashup = current($mashups))
{
$stmt = $dbh->prepare('SELECT `key`, value, displayable
FROM setting WHERE mashup_id = :id');
$stmt->bindParam(':id', $mashup->id, PDO::PARAM_INT);
$stmt->execute();
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'Setting');
}
$stmt->closeCursor();
Run Code Online (Sandbox Code Playgroud)
您$key
的构造函数中有一个非默认参数:
public function __construct($key, $value = null, $displayable = 1)
Run Code Online (Sandbox Code Playgroud)
所以,当你这样做时:
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'Setting');
Run Code Online (Sandbox Code Playgroud)
错误:warning: Missing argument 1 for Setting::__construct() in pdo.php
仅针对参数抛出,$key
因为它不是默认值.
正确使用fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,...
是这样的:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'classname',
<array of parameter names(in order) used in constructor>);
Run Code Online (Sandbox Code Playgroud)
所以,在你的情况下:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'Setting',
array('key', 'value', 'displayable');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4242 次 |
最近记录: |