2
我一直在试图实施一个角色分类模型模式到我的网站用户访问机制(用PHP编写)。但我有一些疑问。下面是相关的代码的简化版本:角色分类模型正确实施
class User
{
public $role;
public $uid;
public function setRole($role)
{
$this->role = $role;
}
}
// role classes responsible for restricted actions
class BaseRole {}
class AdminRole extends BaseRole
{
// do something adminish + log action into database (with User ID)
public function SomethingAdminish($admin_id) { }
}
$user = new User();
$user->setRole(new AdminRole());
// pass Admin ID (User ID) into method
$user->rola->SomethingAdminish($user->uid);
我看到一些弱点在这里:
- 传递任何其他$用户> uid解释为“SomethingAdminish”方法将在我的日志 日志信息不正确系统(错误的用户ID)
如果我决定在上述方法中登录其他用户信息, 基本上我将不得不通过整个用户对象作为参数, 像这样:
$ user-> rola-> SomethingAdminish($ user);
我可能错过了这里必不可少的东西。请你们介绍一下这个问题?