Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete Codeigniter when using get_instance()

Within PHPStorm, I have been able to get autocomplete working with Codeigniter by adding a file to config/autocomplete.php which contains properties. PhpStorm is able to read this file, and allow me to quickly navigate to the function and have autocomplete capabilities. In autocomplete.php, I can have @property Account_model $Account_model, and then whenever I use $this->Account_model->xxx, autocomplete works.

When using get_instance(), all of this functionality dies. For example, when in a helper class, I have to use $CI = & get_instance();, and then $CI->Account_model->xxxx. How do you get autocomplete to work when referencing the library this way?

[Note: this is just a simple example. My true usage is with PHPUnit, but solving the above example will allow PHPUnit to work as well.]

like image 399
justanotherprogrammer Avatar asked Jan 22 '16 16:01

justanotherprogrammer


1 Answers

To re-bind get_instance() to your IDE’s autocomplete, you'll need inform it that’s an instance of the native controller for CodeIgniter. Which isCI_Controller:

/**
 * Example MyClass Library within
 * /application/libraries/
**/
class MyClass {

    /**
     * @var CI_Controller
    **/
    private $ci;

    /**
     * Init MyClass 
    **/ 
    public function __construct() 
    {
        $this->ci =& get_instance();
    }
}
like image 133
MackieeE Avatar answered Oct 11 '22 01:10

MackieeE