I'm trying to create a new instance of Setting
object calling __construct()
method with PHP PDO and constrain PDO::FETCH_PROPS_LATE. Unfortunatly i'm getting this warning (and binding doesn't work).
How can pass column values to the constructor method?
Warning: Missing argument 1 for Setting::__construct() in pdo.php.
Notice: Undefined variable: key in 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();
The constructor specifies $key
parameter as mandatory, because it has no default value provided:
public function __construct(
$key // <---no default value
$value = null,
$displayable = 1
)
So, when you are doing this:
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Setting');
you get warning: Missing argument 1 for Setting::__construct() in pdo.php
.
The error is thrown only for parameter $key
because this has no default value and you aren't providing any.
The correct use of fetchAll
is by providing the optional parameter $constructorArgs
(see available signatures):
$rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'classname',
<array of arguments, with same order used in constructor>
);
So, in your case:
$rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'Setting',
array('your-value-for-key-parameter') // nullable params can be omitted or partially specified, same as for any php function/method
);