问题
I have the Controller:
application/controllers/bob.php
Then in a library I use in the controller Bob.php, I have this code:
$CI =& get_instance();
echo get_class($CI);
So I open the url "domain.com/bob", and I get the correct echo when HMVC is not installed, which is
- Bob
As soon as I install HMVC, the result is:
- CI
So basically, this means I cannot use $CI->someVariableINeed that was declared in Bob because it doesn't exists in CI. Note that $CI->load->helper(), $CI->load->view(), .. are all working however. I tried:
class Bob extends CI_Controller
and
class Bob extends MX_Controller
but it still doesn't work. I've been searching for 24 hours and did not find anything, hopefully someone on SO knows about this. I understand that this is not a very clean way to access members of a class, but this problem is a simplification of an existing project with hundreds of thousands of lines of code, so I cannot change this, the library HAS to access these member variables least I change the whole project.
回答1:
Oh yes!! I'm the man, I fixed it! Thought about maybe creating my "own" get_instance() system, here's what I did:
class MY_Controller extends MX_Controller
{
public static $instance;
function __construct()
{
self::$instance || self::$instance =& $this;
...
}
}
Then in the library, or in the helper or wherever funky place you need to use it:
$CI =& MY_Controller::$instance;
NOTE that if you autoload a library, MY_Controller::$instance won't work if it's in the library's __construct(), as MY_Controller is not defined yet
来源:https://stackoverflow.com/questions/16433210/codeigniter-hmvc-get-instance-in-library-does-not-return-expected-result