库编写语法,如模型/控制器


library writing syntax like model/controller?

使用 CodeIgniter 2,在编写库时:

public function __construct()
{
    $this->ci =& get_instance();
    $this->ci->load->model('one_model');
}
public function test()
{
    $this->ci->one_model->blabla();
}

我对库很好奇,我可以像控制器/模型样式一样编写它(没有 ->ci)吗?

$this->one_model->blabla();

我问这个问题是因为我有时会将模型代码重写为库,以获得更好的逻辑 - db 分离。
任何帮助表示赞赏,谢谢。

只需在类中使用__get函数

class Custom
{   
    public function __construct()
    {
       // construct
    }
    public function __get($var)
    {
       return get_instance()->$var;
    }
    public function test()
    {
        $this->one_model->blabla();
    }
}