use*_*953 2 php codeigniter libraries
我遇到的问题是我试图在codeigniter中使用另一个自定义库中的自定义库.它们都在libraries文件夹中,CodeIgniter告诉我,我必须首先加载CI的实例,我做了...
class MyClass {
public function __construct()
{
$CI =& get_instance();
$CI->load->library("OtherClass");
}
Run Code Online (Sandbox Code Playgroud)
现在在这个类中的函数内部我试图使用我的其他库..
public function my_function()
{
$CI->otherclass->function_inside_this_class();
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: CI
Filename: libraries/MyClass.php
Line Number: 20
Run Code Online (Sandbox Code Playgroud)
在声明codeigniter实例本身方面有什么我缺少的吗?
谢谢!
您的CI变量范围仅限于构造函数.您可以创建一个具有类范围的类变量,并且可以通过$this->variable该类中的所有函数访问它.
class MyClass {
private $_CI; // make a private class variable here.
public function __construct()
{
$this->_CI =& get_instance();
$this->_CI->load->library("OtherClass");
}
public function my_function()
{
$this->_CI->otherclass->function_inside_this_class();
}
Run Code Online (Sandbox Code Playgroud)