Autocomplete Codeigniter when using get_instance ()

In PHPStorm, I was able to get autocomplete working with Codeigniter by adding a file to config / autocomplete.php that contains the properties. PhpStorm can read this file and let me quickly jump to functions 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 used, get_instance()all 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 auto-complete to work when accessing the library this way?

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

+4
source share
2 answers

To relate get_instance()to autocomplete, you need to report that it is an instance of CodeIgniter, which is inturn CI_Controller:

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

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

    /**
     * Init MyClass 
    **/ 
    public function __construct() 
    {
        $this->ci =& get_instance();
    }
}
+1
source

You can try writing properties before running a class like this

<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * @property  usermodel $usermodel
 * @property  groupmodel $groupmodel
 * @property  tablemodel $tablemodel
 * @property  ion_auth $ion_auth
 * @property  db_table_model $db_table_model
 * @property  apimodel $apimodel
 * @property  chartmodel $chartmodel
 * @property  todomodel $todomodel
 * @property  messagemodel $messagemodel
 * @property  photomodel $photomodel
 * @property  settingsmodel $settingsmodel
 * @property  backupmodel $backupmodel
 */
class Ajax extends MX_Controller
{
  public $data;

  public function __construct()
  {
0
source

Source: https://habr.com/ru/post/1625543/


All Articles