使用下面的代码,PDO不会将我的$parcel作为对象返回,而是作为数组返回。在那里,当我试图调用我的对象函数,它会失败。我发现的每一个例子都是这样做的,和我的例子的方式相似。我做错了什么?
$statement = $this->connection->query($query);
$statement->setFetchMode(PDO::FETCH_CLASS, 'Parcel');
while ($parcel = $statement->fetch()) {
echo $parcel->hello();
}
Parcel类,如果感兴趣的话。
class Parcel {
public $id;
public $parcel_number;
public $registred_at;
public $shipped_by;
public $shipped_at;
function __construct($parcel_number)
{
$this->parcel_number = $parcel_number;
}
public function hello(){
return "World";
}
}
我使用文件夹结构来组织代码和自动加载器,这可能会影响PDO调用对象的方式。
而这是错误代码
Call to a member function info() on a non-object
此错误表示对象为空或未正确实例化,您可能需要指定类的完整路径。
例如$statement->setFetchMode(PDO::FETCH_CLASS, 'app'model'Parcel');