【问题标题】:Extending Sentry 2 Models from a Base Eloquent class从基本 Eloquent 类扩展 Sentry 2 模型
【发布时间】:2013-09-04 02:35:55
【问题描述】:
目前我的所有模型都扩展了一个名为 "Base"
的基类
Base 扩展 Eloquent
问题是我使用 Sentry 2 进行用户身份验证和组,因此我的两个模型 User 和 Group 扩展了它们各自的 Sentry 基本模型 SentryUserModel 和 SentryGroupModel。
我想不出一种方法来拥有这两个模型:User, Group 扩展了我的 Base 模型
【问题讨论】:
标签:
php
oop
inheritance
laravel-4
eloquent
【解决方案1】:
您可以让您的 User 和 Group 模型分别扩展 SentryUserModel 和 SentryGroupModel,然后使用魔法函数扩展 Base 模型。
代码:
class User extends SentryUserModel
{
private $base;
function __construct()
{
$this->base = new Base();
}
// fake "extends Base" using magic function
public function __call($method, $args)
{
$this->base->$method($args[0]);
}
}
class Group extends SentryGroupModel
{
private $base;
function __construct()
{
$this->base = new Base();
}
// fake "extends Base" using magic function
public function __call($method, $args)
{
$this->base->$method($args[0]);
}
}