【发布时间】:2021-05-24 11:10:07
【问题描述】:
我创建了一个 Depot 类。当我从此类创建对象时,我使用 find 方法查找具有 id 的特殊项目。
之后我不能调用任何其他方法。
我不使用 Laravel
// index.php file
$depot = new Depot();
$depot = $depot->find(2);
var_dump($depot->hi());
致命错误:未捕获错误:调用未定义方法 stdClass::hi()
hi 方法用于测试。
// model.php file
class Model {
// ...
public function find(int $id)
{
$statement = $this->pdo->prepare("select * from {$this->table} where id = :id");
$statement->execute(compact('id'));
$obj = $statement->fetch(PDO::FETCH_OBJ);
return $obj;
}
}
class Depot extends Model {
//...
public function hi()
{
echo "hi";
}
}
【问题讨论】:
-
find()在这种情况下返回调用PDOStatement::fetch()的结果,我不希望有一个方法hi()- 你用这个结果替换$depot。这似乎不是您的意图 - 检查您用于存储结果的逻辑,并确保它符合您的要求。