库问题中的codeigniter库

时间:2013-09-07 03:39:31

标签: php codeigniter

我试图在libraryB的构造函数中调用libraryB中的libraryA。 我知道我能做到:

$CI =& get_instance();
$CI->load->library('A');
$CI->A->someFunc()

但是,如果不启动它,我就无法致电$CI->A->someFunc() {I}中的任何函数$CI =& get_instance(); $CI->load->library('A');我试图访问someFunc();。在其他情况下,我只能在构造函数$this->load->model('somemodel');中启动它,并使用$this->load->someFunc();在类中的任何函数中调用模型。如何实现相同的功能而不必在我想要使用的每个函数中反复重写上面的代码块?

1 个答案:

答案 0 :(得分:0)

我不是100%明确这是你所要求的(你的问题令人困惑),但你可以将CI实例保存为属性,然后从你班级的任何地方访问它。

class My_library {

    protected $CI;

    public function __construct()
    {
        $this->CI =& get_instance();

        $this->CI->load->library('other_library');
    }

    public function someMethod()
    {
        // Use the library (and the CI object) via the CI property.
        $var = $this->CI->other_library->anotherMethod();
    }
}