Reputation: 444
I am trying to figure out why I am getting this error in my code.
Here's the code:
<?php
namespace Db;
use \PDO\PDOStatement;
function getStmt(string $sql, $pdo): PDOStatement
{
$stmt = $pdo->prepare($sql);
return $stmt;
}
Here's the error:
TypeError: Return value of Db\getStmt() must be an instance of PDO\PDOStatement, instance of PDOStatement returned
The use statement seems to be messing up the checking of the return type. If I get rid of the use statement, the error looks like this, because now it assumes PDOStatment to be a part of the current namespace:
TypeError: Return value of Db\getStmt() must be an instance of Db\PDOStatement, instance of PDOStatement returned
If I get rid of the namespace and the use statement, it doesn't complain, but I want both of those statements, and really don't understand what's wrong with the original code.
Upvotes: -1
Views: 41
Reputation: 12017
Your return type is correct but $pdo->prepare
is returning \PDOStatement
which is not \PDO\PDOStatement
. It would help to give $pdo
a type.
That's why there's no error if you change the return type to \PDOStatement
.
Upvotes: 1