Codeigniter 的 DataMapper:可以实例化模型对象一次并重用吗?

发布于 2024-11-28 12:45:17 字数 913 浏览 0 评论 0原文

我不想在这里发送垃圾邮件或做广告,我重视这个社区和那些乐意放弃时间回答问题的人(比如我自己)。我为 Codeigniter 构建了一个名为 WolfAuth 的身份验证库。目前,它依赖 DataMapper 进行所有数据库交互,直至另行通知。

每个函数当前使用的代码如下所示。

$u = new User;
$u->get_by_id($user_id);

if ( $u->exists() )
{  
    // Do user stuff here  
}

我不完全确定 PHP 如何处理对象实例化,但肯定在 50 个函数中执行此操作对性能不会有好处,如果没有,则会增加不必要的对象实例化。是否可以对我正在使用的每个 DataMapper 模型对象进行全局实例化并在每个函数中重复使用?

所以像下面这样。

class Someclass {

    protected $user;
    protected $group;
    protected $roles;

    public function __construct()
    {
        $this->user  = new User;
        $this->group = new Group;
    }

    public function the_user($user_id)
    {
        $user = $this->user->get_by_id($user_id);

        if ( $user->exists() )
        {
            echo "The user exists!";
        } 
    }

}

我是否只是在这里非常挑剔,或者我所要求的是可能的,并且可能是为我建立的如此大的图书馆做事的更好方法?

Not trying to spam or advertise here, I value this community and the people that gladly give up their time to answer questions (like myself). I have built an authentication library for Codeigniter called WolfAuth. It currently relies on DataMapper for all database interaction, until further notice.

Each function currently uses code along the lines of the following.

$u = new User;
$u->get_by_id($user_id);

if ( $u->exists() )
{  
    // Do user stuff here  
}

I'm not entirely sure how PHP handles object instantiation, but surely doing this in 50 functions plus can't be good for performance, and if not, adds unnecessary object instantiation. Is it possible to have a global instantiation of each DataMapper model object I am using and re-use in each function?

So something like the following.

class Someclass {

    protected $user;
    protected $group;
    protected $roles;

    public function __construct()
    {
        $this->user  = new User;
        $this->group = new Group;
    }

    public function the_user($user_id)
    {
        $user = $this->user->get_by_id($user_id);

        if ( $user->exists() )
        {
            echo "The user exists!";
        } 
    }

}

Am I just being extremely picky here, or is what I am asking possible and probably the better way to do things for such a large library like I've built?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-12-05 12:45:17

是的,您可以在 php 中将对象作为参数传递。

class Someclass {

protected $user;
protected $group;
protected $roles;

public function __construct()
{
    $this->user  = new User();
    $this->group = new Group();
}

public function get_user($user_id)
{
    if (empty($this->user->id))
    {
        $user = $this->user->get_by_id($user_id);
    }
    else
    {
        $user = $this->user;
    }
    if ( $user->exists() )
    {
        prove_user_exists($user);
        return $user;
    }
    else
    {
        show_error('No User Found');
    }
}

public function prove_user_exists($user)
{
    log_message('info', $user->id);
}

然后,当您需要用户时,您可以调用 get_user($user_id) - 如果已经找到用户,那么您将不必再次调用数据库。

Yes, you can pass objects as arguments in php.

class Someclass {

protected $user;
protected $group;
protected $roles;

public function __construct()
{
    $this->user  = new User();
    $this->group = new Group();
}

public function get_user($user_id)
{
    if (empty($this->user->id))
    {
        $user = $this->user->get_by_id($user_id);
    }
    else
    {
        $user = $this->user;
    }
    if ( $user->exists() )
    {
        prove_user_exists($user);
        return $user;
    }
    else
    {
        show_error('No User Found');
    }
}

public function prove_user_exists($user)
{
    log_message('info', $user->id);
}

Then, you can just call get_user($user_id) when you need the user - if the user has already been found, then you won't have to call the db again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文