Bug #75387 [Com]: fetchAll complains about extraneous parameters
Edit report at https://bugs.php.net/bug.php?id=75387&edit=1
ID: 75387
Comment by: keithy at consultant dot com
Reported by: aljosha dot papsch at vinexus dot eu
Summary: fetchAll complains about extraneous parameters
Status: Open
Type: Bug
Package: PDO related
PHP Version: 7.1.10
Block user comment: N
Private report: N
New Comment:
Simple subclassing of PDOStatement and overriding of fetchAll(...) is broken.
Necessitating code like such:
function fetchAll($how = null, $class_name = null, $ctor_args = null) {
if (!isset($ctor_args)) {
if (!isset($class_name)) {
if (!isset($how)) {
return $this->PDOStatement->fetchAll();
}
return $this->PDOStatement->fetchAll($how);
}
return $this->PDOStatement->fetchAll($how, $class_name);
}
return $this->PDOStatement->fetchAll($how, $class_name, $ctor_args);
}
Previous Comments:
------------------------------------------------------------------------
[2018-03-02 10:27:38] aljosha dot papsch at vinexus dot eu
Originally, I just wanted a thin wrapper ironing out some usage issues I perceived with PDO. But you
are right, it's a good opportunity to create a more convenient interface. No need to keep this
bug open.
------------------------------------------------------------------------
[2017-10-18 07:38:57] yannis dot berrouag at gmail dot com
I don't think it's a good idea, you are trying to use a function incorrectly and php warns
you about that, this is a perfectly normal behavior.
if you are writing a wrapper, why don't you just write yourself simpler methods like
fetchAssoc(), fetchBoth() etc ... and make the correct call to fetchAll inside.
------------------------------------------------------------------------
[2017-10-16 10:25:04] aljosha dot papsch at vinexus dot eu
Description:
------------
I created a small wrapper around PDO. My first stab at wrapping fetchAll looked like this:
public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
$fetchArgument = null,
array $constructorArgs) {
return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
}
PDO rejected the method call, complaining:
SQLSTATE[HY000]: General error: Extraneous additional parameters
So I had to turn the simple one liner into:
public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
$fetchArgument = null,
array $constructorArgs = []) {
if ($fetchStyle === \PDO::FETCH_BOTH) {
return $this->st->fetchAll();
}
elseif ($fetchStyle === \PDO::FETCH_CLASS) {
return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
}
elseif ($fetchStyle === \PDO::FETCH_ASSOC ||
$fetchStyle === \PDO::FETCH_NUM ||
$fetchStyle === \PDO::FETCH_ASSOC) {
return $this->st->fetchAll($fetchStyle);
}
else {
return $this->st->fetchAll($fetchStyle, $fetchArgument);
}
}
It would be great if PDO would not complain about extraneous arguments and just ignore arguments not
needed. Otherwise, any PDO wrapper has to do this logic again of its own, possibly with errors. For
example, there is a bug in the code above: I forgot considering \PDO::FETCH_OBJ in the last elseif
block.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75387&edit=1
Thread (5 messages)