【发布时间】:2019-06-25 14:53:48
【问题描述】:
我是面向对象的 PHP 新手,对在哪里实施访问限制有疑问。
我有一些方法可供任何人使用,但有些方法可以更新数据库,并且只能由特定用户级别访问。
类方法是否适合添加该逻辑,或者是否应该保持干净并将该逻辑放在进行方法调用的位置。
$product = new Product($id);
$product->setCategory($category);
public function setCategory($category)
{
if(get_access(5)) {
Item::where('id', $this->id)->update(['category' => $category]);
$this->category = $category;
}
}
或
$product = new Product($id);
if(get_access(5)) {
$product->setCategory($category);
}
public function setCategory($category)
{
Item::where('id', $this->id)->update(['category' => $category]);
$this->category = $category;
}
或
我没有想到的其他方式。
【问题讨论】:
-
你在使用 User 类吗?