我一直在尝试为我的网站用户访问机制(用 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);
我在这里看到了一些弱点:
- 将任何其他 $user->uid 传递给“SomethingAdminish”方法会将不正确的信息记录到我的日志系统中(错误的用户 ID)
如果我决定在上述方法中记录其他用户信息,基本上我必须将整个用户对象作为参数传递,如下所示:
$user->rola->SomethingAdminish($user);
我可能在这里遗漏了一些重要的东西。请问各位大神,能不能给点思路?