使用 get_instance() 时自动完成 Codeigniter

Autocomplete Codeigniter when using get_instance()

在 PHPStorm 中,我已经能够通过向 config/autocomplete.php 添加一个包含属性的文件来使用 Codeigniter 进行自动完成。 PhpStorm 能够读取此文件,并允许我快速导航到该函数并具有自动完成功能。在 autocomplete.php 中,我可以有 @property Account_model $Account_model,然后每当我使用 $this->Account_model->xxx 时,自动完成工作。

使用 get_instance() 时,所有这些功能都会消失。例如,当在 helper class 中时,我必须使用 $CI = & get_instance();,然后是 $CI->Account_model->xxxx。以这种方式引用库时如何使自动完成工作?

[注意:这只是一个简单的例子。我的真实用法是与 PHPUnit 一起使用,但解决上面的示例将使 PHPUnit 也能正常工作。]

你可以尝试在开始之前编写属性 class 像这样

<?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()
  {

要将 get_instance() 重新绑定到您的 IDE 的自动完成,您需要通知它这是 CodeIgniter 的本机控制器实例。即CI_Controller:

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

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

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